(SOLVED) Scored World Jumble - Again Game loop

Hello everyone,

I have written a code for a scored word jumble, however my "play again" option isn't working the way I want it to.

Fact is, when the player guesses a word right, he then decides by entering (y/n) if he wants to play again. But as soon as he presses yes, then the game doesn't keep track of his previous word jumble score and starts over completely.

How can I implement a loop that will keep track of the previous score, or simply go on with the game without giving me that problem?

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Word Jumble Scored

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    string guess;
    int points = 0;
	int wordpoints;
    char another;
    
    enum fields {WORD, HINT, NUM_FIELDS};
    const int NUM_WORDS = 5;
    const int MAX_GUESSES = 5;
    const string WORDS[NUM_WORDS][NUM_FIELDS] =
    {
        {"wall", "Do you feel you're banging your head against something?"},
        {"glasses", "These might help you see the answer."},
        {"labored", "Going slowly, is it?"},
        {"persistent", "Keep at it."},
        {"jumble", "It's what the game is all about."}
    };

  	srand(time(0));
	int choice = (rand() % NUM_WORDS);
    string theWord = WORDS[choice][WORD];  // word to guess
    string theHint = WORDS[choice][HINT];  // hint for word

    string jumble = theWord;  // jumbled version of word
    int length = jumble.size();
    for (int i=0; i<length; ++i)
    {
        int index1 = (rand() % length);
        int index2 = (rand() % length);
        char temp = jumble[index1];
        jumble[index1] = jumble[index2];
        jumble[index2] = temp;
    }

    cout << "\t\t\tWelcome to Word Jumble!\n\n";
    cout << "Unscramble the letters to make a word.\n";
    cout << "Enter 'hint' for a hint. Note: You will lose one point.\n";
    cout << "Enter 'quit' to quit the game.\n\n";


    bool done = false;
   

    do
    {
        choice = (rand() % NUM_WORDS);
        theWord = WORDS[choice][WORD]; // word to guess
        theHint = WORDS[choice][HINT]; // hint for word
        jumble = theWord; // jumbled version of word
        length = jumble.size();
        
            for (int i = 0; i < length; ++i)
        {
        int index1 = (rand() % length);
        int index2 = (rand() % length);
        char temp = jumble[index1];
        jumble[index1] = jumble[index2];
        jumble[index2] = temp;
        }

        
        cout << "The jumble is: " << jumble;
       
        bool match = false;
       
        for(int nIndex = 0; (nIndex < MAX_GUESSES) && !match ; nIndex++)
        {
            cout << "\n\nGuess " << nIndex+1 << ": ";
            cin >> guess;

            if (guess == "hint")
            {
                cout << theHint;
                points--; //subtract 1 point
            }//end if
            else if (guess == "quit")
            {
                 cout << "\nGood bye!\n";
                 break;
            }
            else if (guess == theWord)
            {
                cout << "\nWell done!\n";
                wordpoints = guess.size();
				points += wordpoints; //points = # of letters in word
				cout << "Number Points for this word is " << wordpoints << endl;
				match = true;
            }//end else if
            else
            {
                cout << "Sorry, that is incorrect.";
            }//end else
        }//end for
        cout << "\n\nYour Total Points Are: " << points;
        cout << "\n\n\nWould You Like To Play Again? (y/n): ";
        cin  >> another;
        system("cls");
        if (another == 'n')
        {
           done = true;        
        }            
    } while(!done);

    //system("cls");
    cout << "Thanks for playing!"<< endl;
    system("pause");
    return 0;
}//end main 
Last edited on
Do you want it to keep only 1 previous score? I.e. guess.size() of the previous word only? Or do you want it to keep a list of the previous scores?
I would like to keep a list of the previous scores, until the player quits.
Could anybody help? I'm sort of stuck on where to go.
Did I not do my for loop correctly?
Why is not allowing me to keep track of the player's previous scores until he quits?
Put your code for your previous score into your if( another == 'n' ) statement. If you want just one previous score then use a variable int or double, but if you want multiple scores to be saved then use an array.
Do you mean, keep track of scores from previous games if they wish to continue?

If so make a vector<int> of scores and push_back() the points from that round if they choose continue. At the end, loop through the vector and print out the scores.
Could you possibly pseudocode that?

I'll give a shot to your suggestion right away though.

Thanks
Never mind, I reviewed my code and it actually does work.

It simply does a system clear, so I just got confused by my own code - sigh.

Thanks!
Topic archived. No new replies allowed.