c++代码最后一行出现错误
C++ error at the last line of code
我是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之后缺少了一个结束大括号。其他一切都很好。您还使用=
来比较两个变量,而不是==
, =
是一个赋值运算符,而==
用于比较。
在}
的末尾缺少一个关闭主方法的右括号。
我还会注意针对解决代码中其他问题的问题的注释。
相关文章:
- 课堂上的一行,使整个应用程序坏了. 0xC000005错误
- 从带有 getline() 的文件读入一行上有多个信息得到无效参数错误
- 线程1:EXC_BAD_ACCESS错误,此错误在第一行的CPP文件中的句柄函数中出现
- C++在 while 循环后添加一行代码会导致错误
- 使用ifstreamwhile循环,如何显示输入错误并在下一行继续
- clang format函数和错误检查在同一行
- 用“ strcpy”在2D数组中删除(字符)的一行(字符)时是否存在任何错误
- 最后一行执行后的堆栈错误
- 错误 C2144 和错误 C4430 在同一行上
- 如果语句,可能是错误的,输入后不要移至下一行
- 在Visual Studio中重建我的项目时,我会在同一行错误C2371上遇到2个错误
- scanf() 为最后一行输入提供错误的输出
- 链接器不指出错误;多个定义警告指向同一行
- 如何一次一个单词而不是一行一行地消除错误输入
- c++计算器atof没有错误,但它在中间一行崩溃(52)
- GCC:为什么一行代码中的错误(字符串和NULL的比较)会导致一长串错误消息
- 根据关键字拆分一行(有拼写错误)
- Makefiles:是否有一种通用的方法来显示makefile中的哪一行导致了错误
- 简单的一行类方法返回错误的值
- 编译错误,注释/debug/uncomment/debug在一行上消失