2015-08-09 44 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