如何修复仅在 C++ 中每隔一次调用一次才执行的函数?

问题描述

今年夏天我正在尝试学习基本的 C++,但我正在为这个石头剪刀布游戏程序而苦苦挣扎。

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

所以 getUserChoice 函数似乎每次都会被调用和执行,但可能不是确定Winner 函数

我尝试了很多不同的更改,但似乎找不到错误。任何帮助将不胜感激!

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 Game\n"
        <<"---------\n"
        <<"1) Rock\n"
        <<"2) Paper\n"
        <<"3) Scissors\n"
        <<"4) Quit\n\n"
        <<"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: ROCK\n\n"
            <<"The computer selected: PAPER\n\n"
            <<"Computer wins! Paper wraps rock!\n\n" 
            <<"*********************************\n"<<endl;
    }
    else if(userChoice == 1 && computerChoice == 3)
    {
        cout<<"\nYou selected: ROCK\n\n"
            <<"The computer selected: SCISSORS\n\n"
            <<"You win! Rock smashes scissors!\n\n"
            <<"*********************************\n"<<endl;
    }
    else if(userChoice == 2 && computerChoice == 1)
    {
        cout<<"\nYou selected: PAPER\n\n"
            <<"The computer selected: ROCK\n\n"
            <<"You win! Paper wraps rock!\n\n"
            <<"*********************************\n"<<endl;  
    }
    else if(userChoice == 2 && computerChoice == 3)
    {
        cout<<"\nYou selected: PAPER\n\n"
            <<"The computer selected: SCISSORS\n\n"
            <<"Computer wins! Scissors cut paper!\n\n"
            <<"*********************************\n"<<endl;
    }
    else if(userChoice == 3 && computerChoice == 1)
    {
        cout<<"\nYou selected: SCISSORS\n\n"
            <<"The computer selected: ROCK\n\n"
            <<"Computer wins! Rock smashes scissors!\n\n"
            <<"*********************************\n"<<endl;
    }
    else if(userChoice == 3 && computerChoice == 2)
    {
        cout<<"\nYou selected: SCISSORS\n\n"
            <<"The computer selected: PAPER\n\n"
            <<"You win! Scissors cut paper!\n\n"
            <<"*********************************\n"<<endl;
    }
    else
        cout<<"\nTie,No winner!\n\n"
            <<"*********************************\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 循环中被调用两次:一次在正文中,一次在条件中。

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);