比较用户输入和文本文件

comparing user input with text file

本文关键字:文本 文件 输入 用户 比较      更新时间:2023-10-16

我在做c++。我想比较用户在整个文本文件中输入的内容。
我的文本文件是这样的:

库马尔印度+9102223403434 01/02/1980新加坡航空8118 Elite -

我总共有15个数据项。最重要的是,如果用户没有输入正确的数据,他们只有3次尝试。

这是我的代码。请建议。

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
void Member();
using namespace std;
int main()
{
    int option;
    string member;
    cout<<"tWelcome!n";
    cout<<"n1. Member"<<endl;
    cout<<"2. Non member"<<endl;
    cout<<"Please enter your option."<<endl;
    cin>>option;
    system("cls");
    {
    if (option==1)
    {
        Member();
    }
    else if (option==2)
    {
    cout<<"awesome";
    }
    else
        cout<<"Invalid choice!";
    }
    system("pause");
    return 0;
}
void Member()
{
ifstream inFile;
string userName,userNationality,airline,userType,type,userBirthday,birthday,userContact,Tmiles,userBenefit; 
bool found=false;
for (int i=0; i<3; i++)
{
cout << "Enter birthday: ";
cin>>birthday;
inFile.open("list.txt");
if (!inFile)
cout << "Unable to open file"<<endl;
else
{
while (!inFile.eof())
{
    inFile >> userName >> userNationality >> userContact >> userBirthday >> airline >> Tmiles >> userType >> userBenefit;
if (inFile.fail())
    break;
else if (userBirthday == birthday)
        {
        system("cls");
        cout<<"welcome"<< " " << userName <<"!" << endl;
        found=true;
        break;
        }   
}
}
    }
inFile.close();
    if (!found) //found==false
        cout<<"You have exceed the limit. PLease try again"<<endl;
}

问题是break只退出最内层的循环。

当你break时,你退出while循环,但不退出for循环,所以你会被再次询问生日。

改变这

for (int i=0; i<3; i++)

for (int i=0; i<3 && !found; i++)