所以我必须创建一个随机数猜测游戏。我知道还有其他类似的问题,但如果有人能给我指明正确的方向,我将不胜感激。我对C++很陌生。
我的问题似乎是,在每个难度函数中都没有提取变量。我得到了未声明的错误。
非常感谢。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int easy()
{
srand(seed);
randomNumber = 1 + rand() %12;
cout << "I am thinking of a number between 1 and 12. Can you guess what it is?" << endl;
cout << randomNumber << endl;
cin >> userNumber;
while (!win)
{
if (userNumber < randomNumber)
{
numOfAttempts++;
cout << "Attempt " << numOfAttempts << ". " << userNumber << " is too low. Please try again" << endl;
cin >> userNumber;
playerScore -= 5;
}
else if (userNumber > randomNumber)
{
numOfAttempts++;
cout << "Attempt " << numOfAttempts << ". " << userNumber << " is too high. Please try again" << endl;
cin >> userNumber;
playerScore -= 5;
}
else
{
numOfAttempts++;
cout << "Congratulations. You guessed the correct number. It took you " << numOfAttempts << " attempts." << endl;
cout << "Your score is " << playerScore << endl;
win = true;
}
}
}
int medium()
{
srand(seed);
randomNumber = 1 + rand() %30;
cout << "I am thinking of a number between 1 and 30. Can you guess what it is?" << endl;
cout << randomNumber << endl;
cin >> userNumber;
while (!win)
{
if (userNumber < randomNumber)
{
numOfAttempts++;
cout << "Attempt " << numOfAttempts << ". " << userNumber << " is too low. Please try again" << endl;
cin >> userNumber;
playerScore -= 5;
}
else if (userNumber > randomNumber)
{
numOfAttempts++;
cout << "Attempt " << numOfAttempts << ". " << userNumber << " is too high. Please try again" << endl;
cin >> userNumber;
playerScore -= 5;
}
else
{
numOfAttempts++;
cout << "Congratulations. You guessed the correct number. It took you " << numOfAttempts << " attempts." << endl;
cout << "Your score is " << playerScore << endl;
win = true;
}
}
}
int hard()
{
srand(seed);
randomNumber = 1 + rand() %50;
cout << "I am thinking of a number between 1 and 50. Can you guess what it is?" << endl;
cout << randomNumber << endl;
cin >> userNumber;
while (!win)
{
if (userNumber < randomNumber)
{
numOfAttempts++;
cout << "Attempt " << numOfAttempts << ". " << userNumber << " is too low. Please try again" << endl;
cin >> userNumber;
playerScore -= 5;
}
else if (userNumber > randomNumber)
{
numOfAttempts++;
cout << "Attempt " << numOfAttempts << ". " << userNumber << " is too high. Please try again" << endl;
cin >> userNumber;
playerScore -= 5;
}
else
{
numOfAttempts++;
cout << "Congratulations. You guessed the correct number. It took you " << numOfAttempts << " attempts." << endl;
cout << "Your score is " << playerScore << endl;
win = true;
}
}
}
int main ()
{
public:
int randomNumber;
int userNumber;
bool win = false;
unsigned seed = time(0);
int numOfAttempts = 0;
int playerScore = 50;
int difficulty;
cout << "Please choose a difficulty. 1 = easy, 2 = medium, 3 = hard." << endl;
cin >> difficulty;
switch(difficulty)
{
case 1:
cout << "You have chosen EASY!" << endl;
easy();
break;
case 2:
cout << "You have chosen MEDIUM!" << endl;
medium();
break;
case 3:
cout << "You have chosen HARD!" << endl;
hard();
break;
default:
cout << "You are playing EASY!" << endl;
easy();
break;
}
return 0;
}
您的问题是您在困难函数中使用的变量没有在这些函数中声明。您应该将它们作为参数传递给这些函数
int hard(int randomNumber, unsigned seed)
错误消息非常清楚。例如,变量randomNumber
的声明在哪里?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int easy()
{
srand(seed);
randomNumber = 1 + rand() %12;
^^^^^^^^^^^^
//...
或者这意味着什么?
int main ()
{
public:
int randomNumber;
int userNumber;
bool win = false;
unsigned seed = time(0);
int numOfAttempts = 0;
int playerScore = 50;
int difficulty;
//...
从语法角度来看,该代码无效。你应该纠正它。
通常,错误代码会给您提供相当多的信息(直到您进入模板编码,才有另一条学习曲线)。你的代码有很多错误,甚至在没有为你做所有工作的情况下就开始帮助你。
请复制并粘贴更好地描述实际的错误/警告消息,并不是所有人都有编译器在工作,以了解发生了什么。