在 C++ 中重新启动循环?

Restarting a loop in C++?

我的程序需要帮助。我必须使用 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;
    }
}
#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;
}

你可以使用 goto 语句来做你想做的事情,但是在使用它之前,我们会询问用户是否 he/she 想再玩一次,我们会接受输入,如果 he/she 说 yes(y) 那么我们将重新启动代码,如果 no(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;
   }
}