2014-04-26 4 views
-3

do while 루프에서도 무한 루프가 계속됩니다!무한 루프가 계속 끝나는 이유는 무엇입니까?

내가 뭘 잘못하고 있니? 나는 모든 것을 시도했다. 그러나 나는 그것을 아직도 이해할 수 없다. 어떤 도움?

#include <iostream> 
#include <string> 
#include <Windows.h> 

using namespace std; 

//function prototypes 
//prototype for IsAccessible 
int IsAccessible(string username, string password); 

//prototype for menu 
void menu(); 

int main() 
{ 
    string username; 
    string password; 

    //make user to login 
    cout << "Enter username : "; 
    getline(cin, username); 
    cout << "\nEnter Password : "; 
    cin >> password; 
    IsAccessible(username, password); 
    cout << "Thank you for logging in!!"; 
    cin.ignore(); 
    cin.get(); 
    return 0; 
} 

//function definitions 
//definition for IsAccesible 
int IsAccessible(string username, string password) 
{ 
    //check if user entered correct details 
    do 
    { 
     int x = 0; 
     if(password == "123" && username == "asdqw") 
     { 
      cout << "\n\nThank you for loggin in John!"; 
      break; 
     } 
     //if user entered wrong details 
     else if(password != "123" && username != "asdqw") 
     { 
      cout << "\nYou have either entered a wrong password or username.\n"; 
      cout << "Please retry."; 
     } 
     //if user exceeds limitation 
     if(x == 5) 
     { 
      cout << "\n\nYou have exceeded the 5 retry limitations......\n"; 
      Sleep(4000); 
      cout << "Exiting program...."; 
      Sleep(5000); 
      return 0; 
     } 
    }while(password != "123" && username != "asdqw"); 
    return 0; 
} 
+2

어디에서 while 루프에 입력합니까? –

+0

'cin >> password;를해야합니다.' 루프를 끝내려면 루프에 넣으십시오. – yakoudbz

+0

나는 THX를 어떤 방식 으로든 고쳐 놓았다. – user2782625

답변

2

while 사용자 이름이 "asqdf"하지 않고 암호가 "123"하지 않고 코드가 결코 새로운 이름 & 비밀번호를 요구하지 않을 때까지 반복 계속됩니다, 그래서 여기

내 코드입니다 그것은 무한대로 루핑을 계속할 것입니다. 또한 루프가 반복 될 때마다 x을 증가시키지 않으므로 5- 최대 시도 코드가 실행되지 않습니다.

마지막 팁 - 반환 할 필요가없는 경우 반환 유형 void을 만들 수 있습니다. do-while을 종료하기 위해 돌아 오는 대신 break 문을 사용할 수 있습니다.

관련 문제