为什么我的代码在3轮比赛后无法正确切换播放器名称?

时间:2016-01-18 03:13:27

标签: c++ debugging

所以在我的班上我必须制作一个Numberwang模拟游戏。一切正常,除了2轮后名称没有正确关联的事实。它应该说"第3轮,Player1首先发挥。"然而,它可以作为首先播放的播放器。

# include <iostream>
# include <ctime>
# include <cstdlib>
using namespace std;



bool numberwang(int n)
{
    if(n < 100 ){
        return 1;
    } else {
        return 0;

    }

}
int main()
{
    string Firstplayer, Otherplayer;

    int rounds;
    int  counter = 1;
    int number;
    int win = 18;
    int lose= 1;

    cout << "Hello, and welcome to Numberwang, the maths quiz that simply everyone is talking about!" << endl;

    cout << "What is player 1's name? ";
    cin >> Firstplayer;

    cout << "What is player 2's name? ";
    cin >> Otherplayer;

    cout << "How many rounds? ";
    cin >> rounds;

    cout << "Well, if you're ready, lets play Numberwang!" << endl;


    while(counter <= rounds){
        cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
        while(true){
            cout << Firstplayer << ": ";
            cin >> number;

            if(numberwang(number)){
                counter++;
                if(counter > rounds){
                    cout << "That's Numberwang!" << endl;

                    cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose <<  endl;
                    break;
                }

                cout << "That's Numberwang!" << endl;
                swap(Firstplayer, Otherplayer);
                cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;

            }
            cout << Otherplayer << ": ";
            cin >> number;

            if(numberwang(number)){
                counter++;
                if(counter > rounds){
                    cout << "That's Numberwang!" << endl;

                    cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose <<  endl;
                    break;
                }

                cout << "That's Numberwang!" << endl;
                swap(Firstplayer, Otherplayer);
                cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;

            }

        }
    }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

在你的if语句(第61行)之后你说'Firstplayer',然后你输出'Otherplayer'。名称不匹配。

  

块引用

cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;

        }
        cout << Otherplayer << ": ";
        cin >> number;