c++ - 如何提示用户重新循环整个程序?

标签 c++ loops if-statement

我希望用户在再次玩游戏或结束程序之间做出选择,但是当出现提示时,如果他们按“y”,则会一遍又一遍地重复相同的事情,而不是从头开始整个程序。我试过 while 循环、do/while 循环、if 语句、重新排列代码,但没有任何效果。有什么建议吗?

#include <iostream>
#include <string>
using namespace std;

int main(){
    string animal = "fish";
    string guess;
    char choose = 'Y' ;
    int count = 0;//keeps a running total of how many times the user 
has guessed an answer.
    int limit = 5;//allows user to guess only 5 times, otherwise 
they loose the game.
    bool out_of_guesses = false;//to check whether the user has run 
out of guesses.

    cout << "I am thinking of an animal.\n" << endl;

    do{
        while(animal != guess && !out_of_guesses){//Nested while 
loop inside main loop to keep track of how many tries the user has 
attempted and to validate their answers.
        if(count < limit){
            cout << "Can you guess what animal I am thinking of?: ";
            getline(cin, guess);
            count++;
            if(animal != guess){
                cout << "\nHmm, nope. That's not the animal I'm 
thinking of." << endl;
                if(count > 2 && count <5){
                    cout << "I'll give you a hint. It lives in 
water." << endl;
                }
            }
        }
        else{
            out_of_guesses = true;
        }
    }//End nested while loop
        if(out_of_guesses){
            cout << "\nI'm sorry, but you are out of guesses." << 
endl;
        }
        else{
            cout << "\n*** Good job! You guessed the correct animal! 
***" << endl;
            cout << "\t\t><)))º> ❤ <º)))><\t\t" << endl;
        }

    //The do-while loop is there to ask the user if they wish to 
play the game again.
    cout << "Would you like to try again?(y/n): ";
    cin >> choose;
        if(choose == 'N' || choose == 'n')
            break;
    }while(choose == 'Y' || choose == 'y');
    return 0;
}

最佳答案

bool out_of_guesses = false; 必须介于 while(true)while(animal != guess && !out_of_guesses) 之间, 而不是在第一个 while 循环之外。因为我们的while循环条件一直为假,然后它确实进入了。

您还应该在这两个循环之间重置您的 guess 变量,否则在找到答案的情况下可能会发生同样的事情(false while 循环)。

这里的代码进行了一些重构/审查,我使用猜测作为大写来处理答案的任何排版。我还删除了 out of guess 变量,改为使用 count 和 limit one。

#include <iostream>
#include <string>
#include <cctype>

int main()
{
    const std::string animal = "FISH";
    const int limit = 5;

    do
    {
        std::cout << "I am thinking of an animal.\n";

        int count = 0;
        std::string guess;    

        while(animal.compare(std::toupper(guess)) != 0 && count < limit)
        {
                std::cout << "Can you guess what animal I am thinking of?: \n";
                std::cin >> guess;
                count++;
                if(animal.compare(std::toupper(guess)) != 0)
                {
                    std::cout << "\nHmm, nope. That's not the animal I'm thinking of.\n";
                    if(count > 2)
                    {
                        std::cout << "I'll give you a hint. It lives in water.\n";
                    }
                }
            }
        }//End nested while loop

        if(count >= limit)
        {
            std::cout << "\nI'm sorry, but you are out of guesses.\n";
        }
        else
        {
            std::cout << "\n*** Good job! You guessed the correct animal! ***\n";
            std::cout << "\t\t><)))º> ❤ <º)))><\t\t\n";
        }

        char choose = 'Y' ;
        std::cout << "Would you like to try again?(y/n): ";
        std::cin >> choose;
        if(std::toupper(choose) == 'N') break;

    } while(true);

    return 0;
}

关于c++ - 如何提示用户重新循环整个程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54965600/

相关文章:

C++:在字符串中查找一个字符,然后在前一个字符之后查找另一个字符

c++ - 为什么在 C++ 中允许对 const 全局变量进行多重定义,而在 C 中却不允许?

Java 验证循环未纠正最后一个错误

java - Android - 优化多个if语句

c++ - 混淆结构和结构指针

C++ cout 一个数学函数

perl - 在 perl 中循环更新值

JavaScript循环从数组中随机选择

mysql - MySQL 中的嵌套 if 语句

sql-server - SQL Server : If @variable is null set @variable = x