2015-08-09 97 views
-5

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

库马尔印度9102223403434名1980年1月2日新加坡航空8118精英 -

我一共有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; 
} 
+1

我的建议是问一个问题。除非你问具体问题,并描述什么不适合你,那么你不会得到任何帮助 – john

+0

哦是的。对此感到遗憾。我的问题是我无法比较用户输入。即使我输入了在我的文本文件中找到的数据,它也只是提示输入不同的数据。 – user5207716

回答

0

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

所以当你打破你退出while循环,但不是for循环,所以你会再次被问及生日。

更改此

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

这个

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

我做了更改alr,但它仍然提示我3次,并结束程序,即使我输入了我的文本文件中的内容。 – user5207716

+0

那么好了,然后使用调试器。找出你的代码中发生了什么,而不是猜测。 – john

+0

在调试中显示的内容:'PBL.exe':已加载'D:\ MS6508 \ PBL \ Debug \ PBL.exe',符号已加载。 'PBL.exe':加载'C:\ Windows \ SysWOW64 \ ntdll.dll',加载符号(去除源信息)。 'PBL.exe':加载'C:\ Windows \ SysWOW64 \ KernelBase.dll',加载符号(去除源信息)。 'PBL.exe':加载'C:\ Windows \ SysWOW64 \ msvcp100d.dll',加载符号。 在缓存中找到的应用程序“\ ?? \ C:\ Windows \ system32 \ cmd.exe” 程序'[2416] PBL.exe:Native'已退出,代码为0(0x0)。 – user5207716