[英]Rock, Paper, Scissors, ODD OUTPUT
對於我的課堂項目,我們將制作一個“石頭,剪刀,剪刀布”游戲,使用一個功能來顯示菜單並驗證輸入,並使用一個功能來確定誰贏了。 當我繞過第一個函數時,我的代碼將編譯並正常運行。 但是,當我同時使用功能和I輸入1
, 2
,或3
對我的選擇,它給了我-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";
}
}
}
您沒有處理仍在cin
輸入流中的換行符,也沒有對cin >> selection
進行任何錯誤檢查,因此cin >> selection
失敗。 因為失敗,所以selection
沒有任何新值,並且由於沒有初始化,因此得到的是垃圾編號-4195200
。
你會看到這一點,如果你想進行一些錯誤檢查,或允許的錯誤檢查,你那種已經在你的while
循環通過初始化做的工作selection
,以0
:
int selection = 0;
您還沒有完全將調用choices()
的結果存儲在main()
。 請記住, choices()
的變量selection
是局部變量,並且與main()
的變量selection
,在此代碼中,您永遠不會為其分配值:
selection = choices();
給selection = choices()
而不是choices()
您在函數choices();
定義selection
choices();
作為局部變量,但在main()
輸出全局selection
。
更換
choices();
同
selection = choices();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.