C++ 在'之前应为主表达式;其他';

C++ 在'之前应为主表达式;其他';,c++,C++,我这里有急事。这个作业明天就要交给我的CP1班了。我们必须做一个简单的骰子游戏。如果你得到相同数量的两倍,那么好事就会发生。以下是函数: void Doubles(); //prototype for the function Doubles() //pre: n/a //post: Plays a simple dice game with the user void Doubles() { //variables declared to store dice value

我这里有急事。这个作业明天就要交给我的CP1班了。我们必须做一个简单的骰子游戏。如果你得到相同数量的两倍,那么好事就会发生。以下是函数:

void Doubles();       //prototype for the function Doubles()
//pre: n/a
//post: Plays a simple dice game with the user

void Doubles()
{
    //variables declared to store dice values
    int DieOne, DieTwo, PlayerSame, ComputerSame;

    cout<<"\nLET'S PLAY DOUBLES!!!\n"<<endl;
    srand ( time(NULL) );         //initialize random seed

    DieOne = rand()%6 + 1;
    DieTwo = rand()%6 + 1;

    cout<<"\nYour first die is a "<<DieOne;
    cout<<"\nYour second die is a "<<DieTwo;
    if(DieOne == DieTwo)
    {
       PlayerSame = 1;
    }
    else
    {
        PlayerSame = 0;
    }

    DieOne = rand()%6 + 1;
    DieTwo = rand()%6 + 1;

    cout<<"\n\nThe computer's first die is a "<<DieOne;
    cout<<"\nThe computer's second die is a "<<DieTwo;
    if(DieOne == DieTwo)
    {
        ComputerSame = 1;
    }
    else
    {
        ComputerSame = 0;
    }

    if(PlayerSame == 1 && ComputerSame == 0)
    {
        cout<<"\n\nYou win! Your dice are the same and the "
            <<"computer's dice aren't!";
    }
        else if(PlayerSame == 1 && ComputerSame == 1)
        {
            cout<<"\n\nYou tied! Your dice are the same and the "
                <<"computer's dice are the same!";
        }
        else if(PlayerSame == 0 && ComputerSame == 1)
        {
            cout<<"\n\nYou lost! Your dice are not the same, and the "
                <<"computer's dice are!";
    else
    {
        cout<<"\n\nNeither you nor the computer had dice that matched, "
            <<"so you both lose!";
    }
}
void Doubles()//函数Doubles()的原型
//前:不适用
//post:与用户玩一个简单的骰子游戏
空双()
{
//声明用于存储骰子值的变量
int DieOne、DieTwo、PLAYERNAME、ComputerSame;

您是否忘记了第二个
后面的块的右括号,如果

您忘记了第二个
后面的块的右括号,如果
看起来您缺少了一个括号
}
after
看起来您缺少大括号
}
after
请在else之前添加右大括号

else if(PlayerSame == 0 && ComputerSame == 1)
{
    cout<<"\n\nYou lost! Your dice are not the same, and the "
        <<"computer's dice are!";

}
    else
    {
        cout<<"\n\nNeither you nor the computer had dice that matched, "
            <<"so you both lose!";
    }
else if(PlayerSame==0&&ComputerSame==1)
{

cout请在else之前添加右括号

else if(PlayerSame == 0 && ComputerSame == 1)
{
    cout<<"\n\nYou lost! Your dice are not the same, and the "
        <<"computer's dice are!";

}
    else
    {
        cout<<"\n\nNeither you nor the computer had dice that matched, "
            <<"so you both lose!";
    }
else if(PlayerSame==0&&ComputerSame==1)
{

cout正如已经指出的那样,您缺少大括号,但是该缺陷是由于无法正确查看代码缩进,并且无法清楚地看到错误。else if上的缩进比if缩进多一个,如果不是这样,您将很快看到缺少的大括号,但是,按照格式,它是更容易漏掉,尽管我甚至没有读完整的文章,而是自己看了代码并发现了漏掉的括号(我有很多遗留代码,这些代码有我多年来一直在修复的不良缩进,这是一种常见的格式错误,导致了这个特定的问题。)正确的格式可以更快地发现这类错误,特别是对于那些很久没有编程的程序员在阅读代码细节之前,他们没有经验在头脑中修正格式


Jay

正如已经指出的那样,您缺少大括号,但该缺陷是因为无法正确查看代码缩进,也无法清楚地看到错误。else if上的缩进比if缩进多一个,如果不是这样,您会很快看到缺少的大括号,但是,按照格式,它更容易被遗漏,尽管我在查看代码并找出遗漏的括号之前,甚至没有阅读整篇文章(我有很多遗留代码,这些代码有我多年来一直在修复的不良缩进,这是一种常见的格式错误,导致了这个特定的问题。)正确的格式可以更快地发现这类错误,特别是对于那些很久没有编程的程序员在阅读代码细节之前,他们没有经验在头脑中修正格式


Jay

如果您对编译器错误有疑问,您应该给出编译器给您的完整错误消息,包括行号。进行大括号匹配、语法高亮显示或代码格式化的编辑器也可以帮助您捕获此错误。例如,vim。如果您对编译器错误有疑问,您应该给出t编译器给您的完整错误消息,包括行号。执行大括号匹配、语法高亮显示或代码格式设置的编辑器也可以帮助您捕获此错误。例如,vim。