c++代码最后一行出现错误

C++ error at the last line of code

本文关键字:一行 错误 代码 最后 c++      更新时间:2023-10-16

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

Normal -当你输入一个数字时,程序会告诉你这个数字是大于随机数字还是小于随机数字。

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

一切运行正常,但当我添加一些修复显示的文本程序不会编译。我使用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;
}

工作代码在这里

#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之后缺少了一个结束大括号。其他一切都很好。您还使用=来比较两个变量,而不是==, =是一个赋值运算符,而==用于比较。

}的末尾缺少一个关闭主方法的右括号。

我还会注意针对解决代码中其他问题的问题的注释。