程序跳过一个循环

时间:2014-08-04 05:33:29

标签: c++ while-loop

我目前遇到循环问题。程序首先要求用户输入他的用户名和密码然后进入循环,其中它将输入的用户名和密码与数据库中的数据进行比较。但程序只是跳过while循环并继续下一个语句。

这是我的代码:

string user,pass,access,power , euser, epass;
char ast=' ';
int aste = 0;
ifstream account;
account.open("Account.txt");
cout<<"Username: ";cin.ignore();getline(cin,euser);
for(int i=0;i<euser.length();i++)
{
    euser[i] = toupper(euser[i]);

}
cout<<"Password: ";
do
{
ast = getch();

if (ast == 13)
{
    break;
}

if(epass.size() < 10)
{
     if(ast != 8)
    {
        cout<<"*";
        epass+=ast;
        aste ++;
    }

    else if(ast == 8 && epass.size() > 0)
    {
        cout<<"\b \b";
        epass.erase(epass.size()-1);
    }
}

else if(epass.size() == 10)
{
    if(ast == 8 && epass.size() > 0)
    {
        cout<<"\b \b";
        epass.erase(epass.size()-1);
    }

    else
    {
        cout<<"";
    }
}
}while(ast!=13);


while(account>>user>>pass>>access>>power) // problem loop
{
    cout<<"hi"<<endl;
    if(euser==user && epass==pass)
    {
        system("cls");
        cout<<"Welcome "<<power<<" "<<user;
        break;
    }

    else
    {
        cout<<"\n\nIncorrect username or password. Try again";
        break;
    }
    system("pause");
}

account.close();
system("pause");

1 个答案:

答案 0 :(得分:0)

打破你的代码,使其更容易调试,看看发生了什么:

account >> user;
cout << "user:" << user;
account >> pass;
cout << "pass:" << pass;
account >> access;
cout << "access:" << access;
account >> power;
cout << "power:" << power;
bool b = (bool) account;

while(b)
{
    cout<<"hi"<<endl;
    if(euser==user && epass==pass)
    {
        system("cls");
        cout<<"Welcome "<<power<<" "<<user;
        break;
    }

    else
    {
        cout<<"\n\nIncorrect username or password. Try again";
        break;
    }
    system("pause");
    account >> user;
    cout << "user:" << user;
    account >> pass;
    cout << "pass:" << pass;
    account >> access;
    cout << "access:" << access;
    account >> power;
    cout << "power:" << power;
    b = (bool) account;
}

注意我并不是说这是一个很好的代码,只是它会帮助你了解正在发生的事情并解决你的问题。