简体   繁体   English

预期在“其他”之前的主要表达

[英]expected primary-expression before 'else'

I have an emergency here. 我这有紧急情况。 This homework is due tomorrow for my CP 1 class. 明天我的CP 1课要做这个作业。 We have to make a simple dice game. 我们必须制作一个简单的骰子游戏。 If you get doubles of the same number, then good things happen. 如果您获得相同数字的双打,那么好事就会发生。 Here is the function: 这是函数:

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!";
    }
}

So why, wen I run this, am I getting the compiler error stated in the title? 那么,为什么我运行此命令,却得到标题中指出的编译器错误? the primary expression is right there! 主要的表达就在那里! The "else" it's referring to is the last one there. 它所指的“其他”是那里的最后一个。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

你忘了该块的右大括号后的第二else if

You are missing the brace, as has been pointed out, but the flaw comes from not being able to look at the code indentation correctly and not being able to clearly see the error. 正如已经指出的那样,您缺少括号,但是该缺陷来自无法正确查看代码缩进和无法清晰看到错误。 The indentation on the else if's is indented one more than the if, and if this were not the case, you would quickly see the missing brace, but, as formatted, it is more easily missed, although I did not even read the whole post before looking at the code and spotting the missing brace myself (I have a lot of legacy code which I maintain that has bad indentation that I have been fixing for years, and this is a common type of formatting error that leads to this specific problem.. Proper formatting would lead to the spotting of this type of error much more quickly, especially in programmers that have not been programming for a long time and are not seasoned to fix the formatting in their head as they read through before looking at code specifics. else if的缩进比if缩进一个多,如果不是这种情况,您会很快看到缺失的花括号,但是按照格式设置,它很容易错过,尽管我什至没有阅读整篇文章。在查看代码并亲自找出缺失的花括号之前(我维护了许多遗留的缩进代码,这些缩进代码已经修复了很多年,这是导致这种特定问题的常见类型的格式化错误。正确的格式设置会更快地发现这种类型的错误,尤其是对于那些已经很长时间没有编程并且没有经验的程序员,他们在阅读代码细节之前没有仔细检查过格式。

Jay 周杰伦

Looks like you are missing a brace } after <<"computer's dice are!"; 看起来你缺少一个大括号}之后<<"computer's dice are!"; line. 线。

Doesn't your compiler tell you the line number where it sees the error? 您的编译器不会告诉您看到错误的行号吗?

PLease add the closing brace before the 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!";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM