在'之前应为不合格id;do'; 我想写一个C++程序,允许玩家玩石头剪刀,但是当我尝试编译时,我得到以下错误信息: rps.cpp:50:1: error: expected unqualified-id before 'do'

在'之前应为不合格id;do'; 我想写一个C++程序,允许玩家玩石头剪刀,但是当我尝试编译时,我得到以下错误信息: rps.cpp:50:1: error: expected unqualified-id before 'do',c++,C++,我不知道出了什么事。我的程序粘贴在下面: #include <iostream> #include <cstdlib> #include <ctime> using namespace std; char UserChoice() // takes in input { cout << "Let's play Rock, Paper, Scissors!\n"; cout << " Type in the choic

我不知道出了什么事。我的程序粘贴在下面:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

char UserChoice() // takes in input
{
    cout << "Let's play Rock, Paper, Scissors!\n";
    cout << " Type in the choice you want to make:\n";

    char UserInput = cin.get();

    return UserInput;
}

int ComputerGuess(int SwitchsInput) // generates computer's guess and finds winner
{
    srand((unsigned)time(0));
    int ComputerNum = (rand()%3)+1;
    if (ComputerNum == SwitchsInput)
    {
        cout << "It's a tie!\n";
    }
    else if (ComputerNum == 1 && SwitchsInput == 2)
    {
        cout << "You win! \n";
    }
    else if (ComputerNum == 1 && SwitchsInput == 3)
    {
        cout << "You lose!\n";
    }
    else if (ComputerNum == 2 && SwitchsInput == 1)
    {
        cout << "You lose!\n";
    }
    else if (ComputerNum == 2 && SwitchsInput == 3)
    {
        cout << "You win! \n";
    }
    else if (ComputerNum == 3 && SwitchsInput == 1)
    {
        cout << "You win! \n";
    }
    else if (ComputerNum == 3 && SwitchsInput == 2)
    {
        cout << "You lose!\n";
    }
}
do // do loop allows player to play again
{
    int main()
    {
        char Again = 'y';
        UserChoice();
        switch (UserInput) // validates input and converts to integer
        {
            case 'r':
                ComputerGuess(1);
                break;
            case 'R':
                ComputerGuess(1);
                break;
            case 's':
                ComputerGuess(2);
                break;
            case 'S':
                ComputerGuess(2);
                break;
            case 'p':
                ComputerGuess(3);
                break;
            case 'P':
                ComputerGuess(3);
                break;
            default:
                cout << "That is not valid input!\n";
                UserChoice();
                break;      
        }

        cout << "Want to play again? ENter yes if you do.\n";
        Again = cin.get();
        return 0;
    }
} (while Again == 'y' || Again == 'Y')
#包括
#包括
#包括
使用名称空间std;
char UserChoice()//接受输入
{
cout函数外不能有循环(或其他“做某事”的代码)。请将循环放在
main
内:

// Your other function definitions here

int main() {
    char Again = 'y';
    do {
        // Your code
    } (while Again == 'y' || Again == 'Y')
}

在C++中,C++不需要<代码>返回0;<代码> > <主/ <代码>,在结束控制流时是隐式的。< /p>当我注释循环时,得到的消息与“{”标记在第51行中得到相同的消息。@ USE2532642,<代码> {< /Cord> >属于循环。我将在我的回答中使它更清楚。BTW,查看C++枚举。(预先确定作用域的枚举),它将使代码更易于阅读查找
std::toupper
std::tolower
。通过在
switch
语句之前转换为大写或小写,可以消除一半代码。