我似乎無法弄清楚爲什麼get_password函數調用總是會返回qwert,無論我傳入函數的字符串是什麼。我的問題是我無法看到這個函數中字符串比較出了什麼問題。比較函數調用返回的字符串
string get(string askfor, int numchars, string input)
{
cout << askfor << "(" << numchars << " characters): ";
cin >> input;
return input;
}
string get_password(string name)
{
string pwd;
if (name == "botting"){
pwd = "123456";
}
else if (name == "ernesto") {
pwd = "765432";
}
else if (name == "tong") {
pwd = "234567";
}
else {
pwd = "qwert";
}
return pwd;
}
int main()
{
string name;
string pwd;
string passwd;
cout << "Address of name =" << &name << "\n";
cout << "Address of pwd =" << &pwd << "\n";
cout << "Address of passwd =" << &passwd << "\n";
bool authenticated = false;
while (!authenticated)
{
// call one
string name1 = get("Name", 7, name);
cout << "call one returned: " << name1 << endl;
// call two
string pass1 = get_password(name);
cout << "call two returned: " << pass1 << endl;
//call three
string pass2 = get("Password", 7, passwd);
cout << "call three returned: " << pass2 << endl;
// compare the two passwords
authenticated = false;
if (pass1 == pass2) {
cout << "Welcome " << name << "\n";
authenticated = true;
}
else {
cout << "Please try again\n";
}
}
return 0;
}
代碼混淆'name1'用'name' – tofi9 2015-02-06 03:10:10