C++ 比较函数调用返回的字符串

C++ 比较函数调用返回的字符串,c++,string,oop,C++,String,Oop,我似乎不明白为什么get_password函数调用总是返回qwert,而不管我将什么字符串传递到函数中。我的问题是,我看不出这个函数中的字符串比较出了什么问题 string get(string askfor, int numchars, string input) { cout << askfor << "(" << numchars << " characters): "; cin >> input; ret

我似乎不明白为什么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;
}
stringget(字符串askfor、整数numchars、字符串输入)
{

cout这是因为您从不分配
名称
任何内容。它被初始化为
,并且不会更改。因为它与
获取密码
中的任何情况都不匹配,所以它总是属于
其他
情况,这会产生
“qwert”

我认为问题在于,您的
get
函数最好编写为:

string get(string askfor, int numchars)
{
    string input; // input shouldn't be an argument
    cout << askfor<<"("<<numchars<<" characters): ";
    cin >> input;
    return input;
}

您只需通过引用而不是

string get(string askfor, int numchars, string &input)
{
    cout << askfor << "(" << numchars << " characters): ";
    cin >> input;
    return input;
}
string-get(字符串askfor、整数numchars、字符串和输入)
{

cout将
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;
//呼叫一个
字符串name1=get(“Name”,7,Name);

我是否可以尝试在函数中声明名称并赋值,但程序仍然会使用最后一个else语句字符串get_password(string nameParam){string pwd;string name=nameParam;if(name==“botting”){pwd=“123456”;}else if(name==“ernesto”){pwd=“765432”}else if(name==“tong”){pwd=“234567”}否则{pwd=“qwert”}返回pwd;}@MbusiHlatshwayo您是否尝试了我的建议?然后调用
获取密码(name)
?在调用
获取密码
进行验证之前打印
名称的值。您的代码将
名称1
名称
混淆
    // 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;