比较函数调用返回的字符串
compare strings returned from function calls
我似乎无法弄清楚为什么 get_password 函数调用总是 return 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;
}
那是因为您从未分配 name
任何东西。它被初始化为 ""
,并且永远不会改变。由于匹配 get_password
中的 none 个案例,它总是落入 else
个案例,从而产生 "qwert"
。
我认为问题是您的 get
函数最好这样写:
string get(string askfor, int numchars)
{
string input; // input shouldn't be an argument
cout << askfor<<"("<<numchars<<" characters): ";
cin >> input;
return input;
}
并且您可以使用它的 结果 分配给 name
:
name = get("Name", 7);
您只需要通过 reference 而不是 value。
string get(string askfor, int numchars, string &input)
{
cout << askfor << "(" << numchars << " characters): ";
cin >> input;
return input;
}
将name1
传递给第二个调用:
// call one
string name1 = get("Name", 7, name);
cout << "call one returned: " << name1 << endl;
// call two
string pass1 = get_password(name1); // change this variable
cout << "call two returned: " << pass1 << endl;
get()
returns 名称为 name1
字符串并且不会更新 name
变量本身,因此 name
仍然是空字符串。
我似乎无法弄清楚为什么 get_password 函数调用总是 return 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;
}
那是因为您从未分配 name
任何东西。它被初始化为 ""
,并且永远不会改变。由于匹配 get_password
中的 none 个案例,它总是落入 else
个案例,从而产生 "qwert"
。
我认为问题是您的 get
函数最好这样写:
string get(string askfor, int numchars)
{
string input; // input shouldn't be an argument
cout << askfor<<"("<<numchars<<" characters): ";
cin >> input;
return input;
}
并且您可以使用它的 结果 分配给 name
:
name = get("Name", 7);
您只需要通过 reference 而不是 value。
string get(string askfor, int numchars, string &input)
{
cout << askfor << "(" << numchars << " characters): ";
cin >> input;
return input;
}
将name1
传递给第二个调用:
// call one
string name1 = get("Name", 7, name);
cout << "call one returned: " << name1 << endl;
// call two
string pass1 = get_password(name1); // change this variable
cout << "call two returned: " << pass1 << endl;
get()
returns 名称为 name1
字符串并且不会更新 name
变量本身,因此 name
仍然是空字符串。