2015-01-28 3 views
0

내 텍스트 파일 ("UsernamePassword.txt")에 여러 줄의 사용자 이름과 암호가 있습니다. 2 줄 또는 3 줄의 사용자 이름과 암호를 사용하여 로그인을 시도하면 "유효하지 않은 사용자 이름 또는 암호"부분으로 연결됩니다. 사용자 이름과 비밀번호의 첫 번째 줄만 작동합니다.여러 텍스트 줄을 읽는 방법

여러 줄을 읽는 방법에 대한 제안이 있으십니까?

{ 
    fstream inFile; 
    string user, pass, username, password; 
    int choice; 
    Logo(); 
    cout << endl << endl << endl; 
    inFile.open("UsernamePassword.txt"); 
    if (!inFile) 
     cout << "Unable to Open File"; 
    else 
    {  
     while (username != user) 
     cout << endl << endl << endl; 
     cout << "    Please enter username: "; 
     cin >> user; 
     cout << "    Please enter password: "; 
     cin >> pass; 
     { 
      inFile >> username >> password; 

      if (user == username && pass == password) 
      { 
       system("cls"); 
       cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl; 
       cout << "\t   ****************************************** " << endl; 
       cout << "\t   ** !!! Welcome to CherryLunch !!! ** " << endl; 
       cout << "\t   ****************************************** " << endl; 
       cout<<endl<<endl<<endl<<endl<<endl; 
       system("pause"); 
       system("cls"); 

       MainMenu(); 

      } 
      else 
      {   
       cout<<endl<<endl<<endl<<endl<<endl; 
       cout << "\t    !!! Invalid Username or Password !!!" << endl<<endl; 
       cout << "\t     *** Please try again ***" << endl; 
       cout<<endl<<endl<<endl<<endl<<endl; 

       system("pause"); 
       system("cls"); 
      } 
     } 
    } 
    inFile.close(); 
} 
+2

while 문을 잘못된 줄에 놓았습니까? 또한 텍스트 파일 스 니펫을 표시하십시오. – David

+0

'while (username! = user)'한 줄을'cin >> pass; 아래로 한 줄 옮깁니다. 아마도 그것이 의도 한 일이라고 생각합니다. 그것은 비록 잘못된 것이 아닙니다. –

+0

이것은 [여기에있는 다른 질문] (http://stackoverflow.com/questions/28185382/how-to-stop-a-while-loop)과 관련된 동일한 코딩 오류의 또 다른 증상입니다. 거기에 대한 좋은 대답은 여기에도 적용됩니다. –

답변

0

당신은 우리에게 전체 코드를 부여하지 않은 -하지만 난 당신이 기꺼이이 작업을 수행하고 싶었 여부를 나타내는 코드 (다양한 장소)에 주석을 배치하거나 실수로 그것을했다있다 :

{ 
fstream inFile; 
string user, pass, username, password; 
int choice; 
Logo(); 
cout << endl << endl << endl; 
inFile.open("UsernamePassword.txt"); 
if (!inFile) 
    cout << "Unable to Open File"; 
else 
{  
    while (username != user) // Only the following run will repeat (if this is the intention?) 
    cout << endl << endl << endl; 
    cout << "    Please enter username: "; 
    cin >> user; 
    cout << "    Please enter password: "; 
    cin >> pass; 
    { // This is scoped operation 
     inFile >> username >> password; 

     if (user == username && pass == password) 
     { 

      system("cls"); 
      cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl; 
      cout << "\t   ****************************************** " << endl; 
      cout << "\t   ** !!! Welcome to CherryLunch !!! ** " << endl; 
      cout << "\t   ****************************************** " << endl; 
      cout<<endl<<endl<<endl<<endl<<endl; 
      system("pause"); 
      system("cls"); 

      MainMenu(); 

     } 
     else 
     {   
      cout<<endl<<endl<<endl<<endl<<endl; 
      cout << "\t    !!! Invalid Username or Password !!!" << endl<<endl; 
      cout << "\t     *** Please try again ***" << endl; 
      cout<<endl<<endl<<endl<<endl<<endl; 

      system("pause"); 
      system("cls"); 
     } // End of inner else 
    } // This is the end of scoped operation 
} // End of Outer else 
inFile.close(); 
관련 문제