C ++ Basic“问卷游戏”令人迷惑的错误

时间:2017-01-25 03:38:02

标签: c++ printing conditional

基本上,这个程序应该根据神秘TA的简单是和否答案来询问用户。最后,用户被要求通过输入他们的姓名来猜测TA是谁,如果名称是正确的,则应该打印出来。但是无论我输入什么,我都会获得相同的输出。

我的条件有问题吗?来源如下:

#include <iostream>
#include <string>
using namespace std;

/* define global variabless about TA */
string name = "Connor";

/* set all these as false until they get them right */
bool hairRight = false;
bool gfxArts = false;
bool ownsCat = false;
bool isJunior = false;
bool isGirl = false;
int score = 0;
string answer = ""; //used for user input
void questions()
{
    /* declare local variables */
    bool got_it = false; //right or wrong

    /* start loop */
    do {
        /* Pronmpt user */
        cout << "Does the TA have brown hair? Enter (Y) or (N)" << endl;
        cin >> answer;

        if ( answer == "Y" || answer == "y" ){
                cout << "Yes, you are correct!" << endl;
                hairRight = true; //set answer to correct
                score += 1;
            }else if ( answer == "N" || answer == "n"){
                cout << "I'm sorry, you are incorrect." << endl;
                got_it  = false;
            }else{
                cout << "Oops! I think you mispressed a button!" << endl;
            }

        cout << "Does the TA enjoy graphic design? Enter (Y) or (N)" << endl;
        cin >> answer;

        if ( answer == "Y" || answer == "y" ){
                cout << "Yes, you are correct!" << endl;
                gfxArts = true; //set answer to correct
                score += 1;
            }else if ( answer == "N" || answer == "n"){
                cout << "I'm sorry, you are incorrect." << endl;
                got_it = false;
            }else{
                cout << "Oops! I think you mispressed a button!" << endl;
            }

        cout << "Does this particular Ta enjoy programing?/ does this ta have have a cat? Enter (Y) or (N)" << endl;
        cin >> answer;

        if ( answer == "Y" || answer == "y" ){
                cout << "As far as I know, that's a complete lie..." << endl;
                ownsCat = false; //set answer to correct
                score -= 1;
            }else if ( answer == "N" || answer == "n"){
                cout << "I'm sorry, you are incorrect." << endl;
                got_it  = false;
            }else{
                cout << "Oops! I think you mispressed a button!" << endl;
            }

        cout << "This specific TA is a junior. Enter (Y) or (N)" << endl;
        cin >> answer;

        if ( answer == "Y" || answer == "y" ){
                cout << "Yes, you are correct!" << endl;
                isJunior = true;
                score += 1;
            }else if ( answer == "N" || answer == "n" ){
                cout << "I'm sorry, you are incorrect." << endl;
                got_it = false;
            }else{
                cout << "Oops! this TA has dark brown hair. good try though. " << endl;
            }

        cout << "Who is this mystery TA?" << endl;
        cin >> answer;

        if(got_it == true){
            cout << "Yes, you got it right!" << endl;
            cout << "Score: " << score << "/5" << endl;
        }else {
        cout << "Nope, you're wrong." << endl;
        cout << "Score: " << score << "/5" << endl;
        cout << "Press Enter to Restart" << endl;
        }


        } while (got_it == false);
            /* tell user he/she is wrong */
}

int main()
{
    questions();
    return 0;
}

以下是有问题的代码:

    cout << "Who is this mystery TA?" << endl;
cin >> answer;

if(got_it == true){
    cout << "Yes, you got it right!" << endl;
    cout << "Score: " << score << "/5" << endl;
}else {
cout << "Nope, you're wrong." << endl;
cout << "Score: " << score << "/5" << endl;
cout << "Press Enter to Restart" << endl;
}


} while (got_it == false);
    /* tell user he/she is wrong */

使用它也不起作用:

if(name == "Connor" || name == "connor")

0 个答案:

没有答案