所以我必须创建一个随机数猜测游戏。我知道还有其他类似的问题,但如果有人能指出我正确的方向,我会非常感激。我是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;
}
答案 0 :(得分:1)
您的问题是您在难度函数中使用的变量未在这些函数中声明。 您应该将它们作为参数传递给这些函数
int hard(int randomNumber, unsigned seed)
答案 1 :(得分:0)
错误消息足够清楚。例如,哪里有变量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;
//...
从语法角度来看,代码无效。你应该纠正它。
答案 2 :(得分:0)
通常,错误代码会为您提供相当多的信息(直到您进入模板编码,然后还有另一个学习曲线)。你的代码有很多错误,甚至可以在不为你完成所有工作的情况下开始帮助你。
请复制和粘贴更好地描述实际的错误/警告信息,并非我们所有人都有工作的编译器来查看正在发生的事情。