最后一行代码中的C ++错误

时间:2015-07-14 12:50:55

标签: c++

我是C ++编程的初学者,但我知道基础知识。 我最近开始写一个简单的游戏。该程序选择一个随机数 (1-100),你必须猜测它。有两种模式:

正常 - 每当您输入一个数字程序时,它会告诉您它是否大于随机数或更小。

很难 - 没有线索,只是纯粹的运气。

一切都运行正常但是当我向显示的文本添加一些修复时,程序将无法编译。我使用CODE :: BLOCKS。 屏幕截图:http://scr.hu/81tw/m6cm0 我真的很感激你的帮助。

以下完整代码:

   #include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int number_normal;
int number_hard;
int guess_normal;
int guess_hard;
int tries_normal=0;
int tries_hard=0;
int mode;
int main()
{


{
    cout<<"Choose your mode..."<<endl;
    cout<<"Normal (Press 1) or Hard (Press 2)"<<endl;
    cin>>mode;
    if(mode=1)
        cout<<"Normal mode chosen."<<endl;
            goto normal;
    if(mode=2)
        cout<<"Hard mode chosen!"<<endl;
            goto hard;
            return 0;
}
{

    hard:

        cout<<"I chose a random number in a range from 1 to 100, can you      guess it?"<<endl;
        srand(time(NULL));
        number_hard = rand()%100+1;

    while(guess_hard!=number_hard)

        tries_hard++;
        cout<<"Enter your guess!(Try "<<tries_hard<<"): ";
        cin>>guess_hard;

        if(guess_hard=number_normal)
            cout<<"Respect! You guessed it in "<<tries_hard<<" tries!"<<endl;
}
{

    normal:

        cout<<"I chose a random number from 1 to 100. I will give you some clues! Try to guess it."<<endl;
        srand(time(NULL));
        number_normal = rand()%100+1;

    while(guess_normal!=number_normal)

        tries_normal++;
        cout<<"Enter your guess!(Try "<<tries_normal<<"): ";
        cin>>guess_normal;

        if(guess_normal==number_normal)
            cout<<"Congrats! You're lucky. (Won in "<<tries_normal<<" tries!)"<<endl;

        if(guess_normal<number_normal)
            cout<<"Too low."<<endl;
        else if(guess_normal>number_normal)
            cout<<"That's too much!"<<endl;

        system("pause");
        return 0;
}

2 个答案:

答案 0 :(得分:0)

你在最后}错过了一个关闭大括号的近距离大括号。

我还要注意针对解决代码中其他问题的问题的评论。

注意:从屏幕截图中看不到这一点,说明为什么发布代码的格式不合适。

答案 1 :(得分:0)

有效的代码在这里

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int number_normal;
int number_hard;
int guess_normal;
int guess_hard;
int tries_normal=0;
int tries_hard=0;
int mode;
int main()
{


    {
        cout<<"Choose your mode..."<<endl;
        cout<<"Normal (Press 1) or Hard (Press 2)"<<endl;
        cin>>mode;
        if(mode=1)
            cout<<"Normal mode chosen."<<endl;
                goto normal;
                if(mode=2)
                cout<<"Hard mode chosen!"<<endl;
                goto hard;
                return 0;
    }
{

    hard:

        cout<<"I chose a random number in a range from 1 to 100, can you      guess it?"<<endl;
        srand(time(NULL));
        number_hard = rand()%100+1;

    while(guess_hard!=number_hard)
        {
        tries_hard++;
        cout<<"Enter your guess!(Try "<<tries_hard<<"): ";
        cin>>guess_hard;

        if(guess_hard==number_normal)
            cout<<"Respect! You guessed it in "<<tries_hard<<" tries!"<<endl;
        }
}
{

    normal:

        cout<<"I chose a random number from 1 to 100. I will give you some clues! Try to guess it."<<endl;
        srand(time(NULL));
        number_normal = rand()%100+1;

    while(guess_normal!=number_normal)
        {
        tries_normal++;
        cout<<"Enter your guess!(Try "<<tries_normal<<"): ";
        cin>>guess_normal;

        if(guess_normal==number_normal)
            cout<<"Congrats! You're lucky. (Won in "<<tries_normal<<" tries!)"<<endl;

        if(guess_normal<number_normal)
            cout<<"Too low."<<endl;
        else if(guess_normal>number_normal)
            cout<<"That's too much!"<<endl;

        //system("pause");
    }
}
    return 0;
}

嗯,你没有在你的while循环中使用大括号,并且正如@cowls所建议的那样,在main之后有一个右大括号丢失了。其他一切都很好。您还使用=来比较变量,而不是===是赋值运算符,而==用于比较。