gpt4 book ai didi

c++ - 石头,布,剪刀,奇数输出

转载 作者:行者123 更新时间:2023-11-27 23:14:02 26 4
gpt4 key购买 nike

对于我的类(class)项目,我们要制作一个“石头剪刀布”游戏,使用一个函数来显示菜单和验证输入,以及一个函数来确定谁赢了。当我绕过第一个函数时,我的代码将编译并正常运行。但是,当我同时使用这两个函数并输入 123 进行选择时,它会给我 -4195200 作为一个值。

有什么帮助吗?这是我的代码:

// This project will be a game of Rock, Paper, Scissors

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

// Function Prototypes
int choices();
int userselect (int, int);

int main()
{

//Variables and Seed
int selection;
char again;
unsigned seed = time(0);
srand(seed);
int compselect = (rand()%3)+1;

//Constants for the menu choices
const int ROCK = 1,
PAPER = 2,
SCISSORS = 3;

do
{
//Display the menu choices and validate
choices();

cout << "You picked " << selection << endl;
cout << "The computer picked " << compselect << endl;

//Display the results
userselect(selection, compselect);

//Replay loop
cout << "Do you want to play again? (Y/N)";
cin >> again;
}
while (again == 'Y' || again == 'y');
return 0;
}

//***********************************************************
// This function will display the menu choices and validate *
//***********************************************************

int choices()
{
int selection;

cout << "********************************\n";
cout << "* Please Select A Choice *\n";
cout << "* 1 - Rock *\n";
cout << "* 2 - Paper *\n";
cout << "* 3 - Scissors *\n";
cout << "********************************\n";
cin >> selection;

//Validate the selection
while (selection < 1 || selection > 3)
{
cout << "ERROR: Enter a valid choice.";
cin >> selection;
}

return selection;
}

//***********************************************************
// This function will display the menu choices and validate *
//***********************************************************

int userselect (int num1, int num2)
{
// If user enters Rock
if (num1 == 1)
{
if (num2 == 1)
{
cout << "You picked Rock \n";
cout << "The computer picked Rock \n";
cout << " TIE! \n";
}
else if (num2 == 2)
{
cout << "You picked Rock \n";
cout << "The computer picked Paper \n";
cout << "Paper beats Rock. YOU LOSE! \n";
}
else if (num2 == 3)
{
cout << "You picked Rock \n";
cout << "The computer picked Scissors \n";
cout << " Rock beats Scissors. YOU WIN! \n";
}
}

// If user enters Paper
if (num1 == 2)
{
if (num2 == 1)
{
cout << "You picked Paper \n";
cout << "The computer picked Rock \n";
cout << " Paper beats Rock. YOU WIN! \n";
}
else if (num2 == 2)
{
cout << "You picked Paper \n";
cout << "The computer picked Paper \n";
cout << " TIE! \n";
}
else if (num2 == 3)
{
cout << "You picked Paper \n";
cout << "The computer picked Scissors \n";
cout << " Scissors beats Paper. YOU LOSE! \n";
}
}

// If user enters Scissors
if (num1 == 3)
{
if (num2 == 1)
{
cout << "You picked Scissors \n";
cout << "The computer picked Rock \n";
cout << " Rock beats Scissors. YOU LOSE! \n";
}
else if (num2 == 2)
{
cout << "You picked Scissors \n";
cout << "The computer picked Paper \n";
cout << "Scissors beat Paper. YOU WIN! \n";
}
else if (num2 == 3)
{
cout << "You picked Scissors \n";
cout << "The computer picked Scissors \n";
cout << " TIE! \n";
}
}
}

最佳答案

你不是handling the newline that's still in the cin input stream ,并且您没有对因此失败的 cin >> 选择 进行任何错误检查。因为它失败了,selection 没有被赋予任何新值,而且由于您没有初始化它,您得到这个垃圾号码 -4195200

如果您执行了一些错误检查,或者允许您在 while 循环中通过初始化 selection 完成其工作的错误检查,您就会看到这一点0:

int selection = 0;

main() 中,您也完全无法存储调用 choices() 的结果。请记住,choices() 中的变量 selection 是一个局部变量,与 main() 中的变量 selection 不同),您永远不会为其分配值,在此代码中:

selection = choices();

关于c++ - 石头,布,剪刀,奇数输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17894500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com