如果没有输入“密码”,代码会询问用户的名字。如果找到该名称,则将其名称指定为用户。C++不能用键盘输入
代码打击是一个更大的简化版本,但问题是完全一样的。在else语句中,std::cout << "Hello " << FirstName << std::endl;
无限重复。如果我删除std::cout << "Hello " << FirstName << std::endl;
我无法输入任何内容。就好像我的键盘被拔掉了一样。尝试使用goto,个案,甚至制作一个功能,但问题仍然存在。有任何想法吗?
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::string User;
std::string text;
std::string PassWord;
std::cin >> PassWord;
if(PassWord == "password")
{
User = "Ryan";
}
else if (PassWord != "password")
{
std::string line;
std::string LastName;
std::string FirstName;
std::string AttemptUser;
std::ifstream NameFile;
int offset;
std::cout << "Name?" << std::endl;
NameFile.open("C:\\Filepath\\Names.txt");
if(NameFile.is_open())
{
while(!NameFile.eof())
{
while(std::cin >> FirstName >> LastName)//<---Needing to press Ctrl-z still a problem!
AttemptUser = FirstName + ' ' + LastName;
if((offset = line.find(AttemptUser, 0)) != std::string::npos)
{
NameFile.close();
std::cout << "Hello " << FirstName << std::endl;
User = FirstName;
}
}
}
}
do
{
std::getline(std::cin, text);
if(text.find("the code") != std::string::npos)
{
std::cout << "Yes " << User << ", this is the code" << std::endl;
}
}
while (text != "close");
system("pause");
return 0;
}
你有没有试过设置一个断点并逐步查看会发生什么?你问过你的教授吗? “goto”是邪恶的。此外,你从来没有真正阅读文件,只是打开和关闭它。 – 2014-12-05 21:44:16
有VS 2012 Express所以不能一步一步通过。请详细说明我没有真正阅读文件。可能是我的问题。 – opmxvzasdt 2014-12-05 22:33:12
你可以在4行上引用'NameFile'(顺便说一下,停止在局部变量中大写第一个字符 - 不是很好的做法)。声明,'if(NameFile.is_open())','while(!NameFile.eof())'和NameFile.close()'。在任何时候你都没有从文件中读取数据,所以'.eof()'如果文件存在并且不为空,它将会总是*为假。 – 2014-12-05 23:13:33