0
使用C++程序。想制作一个用户名“checker”。在while循环中处理ifstream。我遇到的问题是,如果用户名不存在,那么它将每次打印错误消息以获取文本中的行数。我知道问题出在while循环中。我不知道如何在没有检查文件的用户名的情况下给出错误信息。任何帮助将不胜感激。谢谢!C++文件输入流循环问题
string username;
string password;
int input;
bool keepGoing;
while (keepGoing){
cout<<("1.Login\n2.Create Username and Password\n3.Exit")<<endl;
cin>>input;
///////////////////////////////////////////////////////////
if(input == 1){ //LOGIN!!!
//open the file
ifstream user("userinfo.txt");
if(user.is_open()){
//get the username
string checkUser;
string checkPass;
cout<<"Enter username: "<<endl;
cin>>checkUser;
//create a variable to store existing username
//Iterate throught the text file, and log them in if thier info is correct
//while(user>>username>>password){
while(getline(user, username)){
//if the name is there
if (checkUser != username){
cout<<"Username not here!"<<endl;
}
if (checkUser==username){
//cout<<"Welcome back, "<<username<<endl;
cout<<"Password: "<<endl;
cin>>checkPass;//get user input
if(checkPass==password){//if the password is correct
cout<<"Welcome back, "<<username<<endl;//User is logged in
//put in the menu 2 function
}else if(checkPass!=password){//If pass is incorrect
cout<<"Password incorrect."<<endl;//Denied
}//end else if
}//end if
}//end while loop
}
else{
cout<<"Unable to open file"<<endl;
}
}