【发布时间】:2026-01-22 20:20:02
【问题描述】:
在这里做学校项目并遇到问题 我的程序支持用户猜测一个数字 47,如果他们猜错了,他们总共有 5 次尝试。如果仍然没有得到它,程序将退出。如果他们得到它,将会有一条消息说它需要多少次尝试才能得到答案。 因此,我必须有一个尝试次数的标识符。但是 C++ 说我没有初始化标识符。请帮忙
#include <iostream>
using namespace std;
int main()
{
int intSecretNum = 47;
int intGuess;
int GuessNum;
cout<< "Guess the secret number (between 0 and 100 inclusively): ";
cin>> intGuess;
while(intSecretNum != 47, GuessNum<= 5)
{
if( intGuess < intSecretNum)
cout << "Your Number is smaller than the secret number";
else if (intGuess > intSecretNum)
cout << "Your Number is bigger than the secret number";
GuessNum++;
if(GuessNum>5)
cout << "Sorry, you have used up all your quota (5 times)! The secret number is "<<intSecretNum;
cout << "program terminated." <<endl;
}
cout<< "You have used "<<GuessNum <<" to guess te secret number which is " <<intSecretNum<<".";
cout<<"program terminated."<<endl;
return 0;
}
请帮忙=D
【问题讨论】:
-
编译器是对的。
GuessNum未初始化。此外,while (intSecretNum != 47, GuessNum <= 5)不正确。使用&&代替逗号。
标签: c++ syntax-error