【问题标题】:Restarting a loop in C++?在 C++ 中重新启动循环?
【发布时间】:2022-01-22 04:38:10
【问题描述】:

我的计划需要帮助。我必须使用 10 个关键字编写一个三轮单词打乱程序,这些关键字将出现在用户面前,让他们猜测。我的问题是,在一个字之后,代码只是简单地退出循环。我的意图是在退出之前再次使用循环第二次和第三次。

代码如下:

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

int main() {
    while (true) {
        enum fields { KEY, HINT, Locked };
        const int NUM_WORDS = 10;
        const string WORDS[NUM_WORDS][Locked] = {

            {"MITCHELL", "NICKNAME IS MITCH,SO WHAT IS MY NAME?"},
            {"PIZZA", "MY FAVORITE FOOD, IS ITALY."},
            {"ROYCE", "SOME TUMBLE AND SOME ROLLS...?"},
            {"INFINITY", "THE IMAGINARY NUMBER IS?"},
            {"AMY", "FAVORITE SONIC CHARACTER IS ?"},
            {"FIAT", "CRYPTO OVER; THIS TYPE OF CURRENCY?"},
            {"RUSSIA", "SAINT PETERSBURG IS IN ?"},
            {"DONETELLO", "TEENAGE MUTANT NINJA TURTLES"},
            {"SON", "BROOKLYN PEOPLE SAY THIS WORD"},
            {"FUN", "FEELING HAPPY"}

        };
        srand(static_cast<unsigned int>(time(0)));
        int choice = (rand() % NUM_WORDS);
        string theWord = WORDS[choice][KEY];   // GUESSING THE WORDS
        string theHint = WORDS[choice][HINT];  // Hint for Words

        // Randomizing using the string; swapping charcters equal to the length of the words
        string Jumble = theWord;  // jumbled version of word
        int length = Jumble.size();
        for (int i = 0; i < length; ++i) {
            int index1 = (rand() % length);  // Random
            int index2 = (rand() % length);  // Random
            char temp = Jumble[index1];
            Jumble[index1] = Jumble[index2];  // Swapping charcters
            Jumble[index2] = temp;
        }
        cout << "\t\t\Welcome to Word Jumble!\n\n";  // USING CARRIAGE RETURN; USING TITLE
        cout << "Unscramble the letters to make a word.\n";
        cout << "Enter 'hint' for a hint.\n";
        cout << "Enter 'quit' to quit the game.\n\n";
        cout << "The jumble is:" << Jumble;
        string guess;
        cout << "\n\nYour guess:";
        cin >> guess;

        while ((guess != theWord) && (guess != "quit")) {
            if (guess == "hint") {
                cout << theHint;
            } else {
                cout << "Sorry, that's not it.";
            }
            cout << "\n\nYour guess:";
            cin >> guess;
        }
        if (guess == theWord) {
            cout << "\nTHat's it! You guessed it!\n";
        }

        cout << "\nThanks for playing.\n";
        return 0;
    }
}

【问题讨论】:

  • 第一个单词被解读后应该有另一个迭代
  • Tangential: Don't call srand repeatedly 如果你要使用它的话。
  • 从 while 正文中提取 return 0;

标签: c++ loops error-handling patch


【解决方案1】:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;

int main() {
    while (true) {
        enum fields { KEY, HINT, Locked };
        const int NUM_WORDS = 10;
        const string WORDS[NUM_WORDS][Locked] = {

            {"MITCHELL", "NICKNAME IS MITCH,SO WHAT IS MY NAME?"},
            {"PIZZA", "MY FAVORITE FOOD, IS ITALY."},
            {"ROYCE", "SOME TUMBLE AND SOME ROLLS...?"},
            {"INFINITY", "THE IMAGINARY NUMBER IS?"},
            {"AMY", "FAVORITE SONIC CHARACTER IS ?"},
            {"FIAT", "CRYPTO OVER; THIS TYPE OF CURRENCY?"},
            {"RUSSIA", "SAINT PETERSBURG IS IN ?"},
            {"DONETELLO", "TEENAGE MUTANT NINJA TURTLES"},
            {"SON", "BROOKLYN PEOPLE SAY THIS WORD"},
            {"FUN", "FEELING HAPPY"}

        };
        srand(static_cast<unsigned int>(time(0)));
        int choice = (rand() % NUM_WORDS);
        string theWord = WORDS[choice][KEY];   // GUESSING THE WORDS
        string theHint = WORDS[choice][HINT];  // Hint for Words

        // Randomizing using the string; swapping charcters equal to the length of the words
        string Jumble = theWord;  // jumbled version of word
        int length = Jumble.size();
        for (int i = 0; i < length; ++i) {
            int index1 = (rand() % length);  // Random
            int index2 = (rand() % length);  // Random
            char temp = Jumble[index1];
            Jumble[index1] = Jumble[index2];  // Swapping charcters
            Jumble[index2] = temp;
        }
        cout << "\t\t\Welcome to Word Jumble!\n\n";  // USING CARRIAGE RETURN; USING TITLE
        cout << "Unscramble the letters to make a word.\n";
        cout << "Enter 'hint' for a hint.\n";
        cout << "Enter 'quit' to quit the game.\n\n";
        cout << "The jumble is:" << Jumble;
        string guess;
        cout << "\n\nYour guess:";
        cin >> guess;

        while ((guess != theWord) && (guess != "quit")) {
            if (guess == "hint") {
                cout << theHint;
            }
            else {
                cout << "Sorry, that's not it.";
            }
            cout << "\n\nYour guess:";
            cin >> guess;
        }
        if (guess == theWord) {
            cout << "\nTHat's it! You guessed it!\n";
        }

        cout << "\nThanks for playing.\n";
       
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    您可以使用 goto 语句来执行您想要做的事情,但在使用它之前,我们会询问用户他/她是否想再玩一次,如果他/她说是(y),我们将接受输入然后我们将重新启动代码,如果没有(n)则退出。 最终代码:

    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
       char res;;
       x:
       while (true) {
          enum fields { KEY, HINT, Locked };
          const int NUM_WORDS = 10;
          const string WORDS[NUM_WORDS][Locked] = {
    
                {"MITCHELL", "NICKNAME IS MITCH,SO WHAT IS MY NAME?"},
                {"PIZZA", "MY FAVORITE FOOD, IS ITALY."},
                {"ROYCE", "SOME TUMBLE AND SOME ROLLS...?"},
                {"INFINITY", "THE IMAGINARY NUMBER IS?"},
                {"AMY", "FAVORITE SONIC CHARACTER IS ?"},
                {"FIAT", "CRYPTO OVER; THIS TYPE OF CURRENCY?"},
                {"RUSSIA", "SAINT PETERSBURG IS IN ?"},
                {"DONETELLO", "TEENAGE MUTANT NINJA TURTLES"},
                {"SON", "BROOKLYN PEOPLE SAY THIS WORD"},
                {"FUN", "FEELING HAPPY"}
    
          };
          srand(static_cast<unsigned int>(time(0)));
          int choice = (rand() % NUM_WORDS);
          string theWord = WORDS[choice][KEY];   // GUESSING THE WORDS
          string theHint = WORDS[choice][HINT];  // Hint for Words
    
          // Randomizing using the string; swapping charcters equal to the length of the words
          string Jumble = theWord;  // jumbled version of word
          int length = Jumble.size();
          for (int i = 0; i < length; ++i) {
                int index1 = (rand() % length);  // Random
                int index2 = (rand() % length);  // Random
                char temp = Jumble[index1];
                Jumble[index1] = Jumble[index2];  // Swapping charcters
                Jumble[index2] = temp;
          }
          cout << "\t\t\Welcome to Word Jumble!\n\n";  // USING CARRIAGE RETURN; USING TITLE
          cout << "Unscramble the letters to make a word.\n";
          cout << "Enter 'hint' for a hint.\n";
          cout << "Enter 'quit' to quit the game.\n\n";
          cout << "The jumble is:" << Jumble;
          string guess;
          cout << "\n\nYour guess:";
          cin >> guess;
    
          while ((guess != theWord) && (guess != "quit")) {
                if (guess == "hint") {
                   cout << theHint;
                } else {
                   cout << "Sorry, that's not it.";
                }
                cout << "\n\nYour guess:";
                cin >> guess;
          }
          if (guess == theWord) {
                cout << "\nTHat's it! You guessed it!\n";
          }
          cout<<"Do you want to play again?(y/n)";
          cin>>res;
          if (res=='y'||'Y'){
             goto x;
             }
          else{
             cout << "\nThanks for playing.\n";
          }
          
          return 0;
       }
    }
    

    【讨论】:

    • 不要建议 goto 循环。
    • 为什么不在循环参数中使用 goto 语句?
    猜你喜欢
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 2010-10-04
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    相关资源
    最近更新 更多