0

I am in need of assistance with my program. I have to code a three-round word scramble program using 10 keywords that will appear scrambled to the user for them to guess it. My problem is that after one word the code just simply exits the loop. My intention is for the loop to be used again for a second and third time before exiting.

Here is the code:

#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;
    }
}
S4eed3sm
  • 1,398
  • 5
  • 20

2 Answers2

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;
}
-1

You can use goto statement for what you are wanting to do, but before using it, we will ask the user if he/she want to play it again, we will take input, if he/she says yes(y) then we will restart the code, if no(n) then exit. The final code:

#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;
   }
}
Harsh404_
  • 15
  • 4