如何修复一个函数,只执行每隔一次它是在c++中调用?



今年夏天我正在努力学习基本的c++,我正在努力学习这个石头剪子布游戏程序。

由于某种原因,当我运行程序时,它只在do-while循环迭代时工作。所以程序第一次正确执行时,第二次打印菜单选项,但当用户回答时,它只是再次显示菜单。然后它在第三次迭代中工作,但在第四次迭代中不工作,以此类推。

所以getUserChoice函数似乎每次都被调用并执行,但可能不是determineWinner函数?

我已经尝试了很多不同的变化,但我似乎找不到错误。任何帮助将非常感激!

int getComputerChoice(int computerChoice)
{
//declare vars for min and max of random number
const int MIN = 1;
const int MAX = 3;

//get system time
unsigned seed = time(0);
//randomize rand
srand(seed);
// Generate random number
computerChoice = MIN + rand() % MAX;

return computerChoice;
}
int getUserChoice(int userChoice)
{   
//declare constants for menu choices
const int ROCK = 1, PAPER = 2, SCISSORS = 3, QUIT = 4;

cout<<"Rock, Paper, Scissors Gamen"
<<"---------n"
<<"1) Rockn"
<<"2) Papern"
<<"3) Scissorsn"
<<"4) Quitnn"
<<"Enter your choice:n";
cin>>userChoice;

//validate input
while (userChoice <1 || userChoice>4)
{
cout<<"Invalid selection. Enter 1, 2, 3, or 4:n";
cin>>userChoice;
}

return userChoice;      
}
void determineWinner(int userChoice, int computerChoice)
{

if(userChoice == 1 && computerChoice == 2)
{
cout<<"nYou selected: ROCKnn"
<<"The computer selected: PAPERnn"
<<"Computer wins! Paper wraps rock!nn" 
<<"*********************************n"<<endl;
}
else if(userChoice == 1 && computerChoice == 3)
{
cout<<"nYou selected: ROCKnn"
<<"The computer selected: SCISSORSnn"
<<"You win! Rock smashes scissors!nn"
<<"*********************************n"<<endl;
}
else if(userChoice == 2 && computerChoice == 1)
{
cout<<"nYou selected: PAPERnn"
<<"The computer selected: ROCKnn"
<<"You win! Paper wraps rock!nn"
<<"*********************************n"<<endl;  
}
else if(userChoice == 2 && computerChoice == 3)
{
cout<<"nYou selected: PAPERnn"
<<"The computer selected: SCISSORSnn"
<<"Computer wins! Scissors cut paper!nn"
<<"*********************************n"<<endl;
}
else if(userChoice == 3 && computerChoice == 1)
{
cout<<"nYou selected: SCISSORSnn"
<<"The computer selected: ROCKnn"
<<"Computer wins! Rock smashes scissors!nn"
<<"*********************************n"<<endl;
}
else if(userChoice == 3 && computerChoice == 2)
{
cout<<"nYou selected: SCISSORSnn"
<<"The computer selected: PAPERnn"
<<"You win! Scissors cut paper!nn"
<<"*********************************n"<<endl;
}
else
cout<<"nTie, No winner!nn"
<<"*********************************n"<<endl;

}   
int main()
{   
//declare vars
int userChoice, computerChoice;
do
{
//call determineWinner function
determineWinner(getUserChoice(userChoice), getComputerChoice(computerChoice));
}
while (getUserChoice(userChoice) != 4);

system ("pause");
return 0;
}

getUserChoice在每个while循环中被调用两次:一次在body中,一次在condition中。

do
{
//call determineWinner function
determineWinner(
getUserChoice(userChoice), // Called once here... 
getComputerChoice(computerChoice));
}
while (getUserChoice(userChoice) // And again here. 
!= 4);

您可以考虑将返回值保存到一个变量中,以便您可以重用它而无需再次提示用户:

do
{
userChoice = getUserChoice(userChoice);
//call determineWinner function
determineWinner(userChoice, getComputerChoice(computerChoice));
}
while (userChoice != 4);

最新更新