Counting system

I know I'm missing something simple is this but I cannot see it, I'm trying to make a counting system so that if the word is 'wall' and you guess it correctly, you get 10 points for each letter in the word so you would get 40 points for the word wall. If you ask for a hint you get 10 points deducted. My problem is that every time the user wants to play again, the total points is the points from the previous word.

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
  int main()
{
	int score = 0;
	int totalScore = 0;
	enum fields{ WORD, HINT, NUM_FIELDS };
	const int NUM_WORDS = 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." }
	};

		cout << "\t\t\tWELCOME TO WORD JUMBLE!\n\n";
		cout << "Unscramble the letters to make a word" << endl;
		cout << "Enter 'hint' for a hint" << endl;
		cout << "Enter 'quit' to quit the game" << endl;
		cout << "\nYou will recieve 10 points for each letter the word has if you guess correctly.";
		cout << "\nNote that if you want a hint, you will lose 10 points." << endl;

		char playAgain = 'y';

		do
		{
			srand(static_cast<unsigned int>(time(0)));
			int choice = (rand() % NUM_WORDS);
			string theWord = WORDS[choice][WORD];
			string theHint = WORDS[choice][HINT];

			string jumble = theWord;
			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 << "\nTotal Score: " << (totalScore + score) << endl;
			cout << "The jumble is: " << jumble << endl;

			string guess;
			cout << "\nEnter your guess: ";
			cin >> guess;

			if (guess == "quit")
			{
				cout << "Bye." << endl;
				return 0;
			}

			while ((guess != theWord))
			{
				if (guess == "hint")
				{
					cout << theHint;
					score -= 10;
					cout << "\nScore: " << score << endl;
				}
				else
				{
					cout << "Sorry, that is not it.";
				}
				cout << "\n\nYour guess: ";
				cin >> guess;
			}

			if (guess == theWord)
			{
				cout << "\nThat's it! You guessed it!" << endl;
				score = theWord.size() * 10;
				cout << "Score: " << score << endl;
			}

			cout << "Do you want to play again? (y/n)" << endl;
			cin >> playAgain;
			
		} while (playAgain == 'y');
Line 27: You don't want to call srand inside your loop. Doing so will reset the random number generator and give you the same sequence of numbers each time through the loop. Move line 27 to line 15.

Where do you update totalScore?

Line 43: You're displaying totalScore + score (from previous round), but totalScore is still 0 (never updated) and the score from the previous round.
alright I got the total score to update now, but now when I get to the hint part, if I enter hit right away it will show score =-10. Then if I enter the right word like 'wall', it gives 40 points instead of 30


This is what I did to fix the totalScore
1
2
3
4
5
6
do
		{
			totalScore += score;
			int choice = (rand() % NUM_WORDS);
			string theWord = WORDS[choice][WORD];
			string theHint = WORDS[choice][HINT];
Last edited on
do I need to have a if statement inside the
 
if (guess == theWord)


I mean an if statement saying if the user used a hint subtract 10 from the score
It's a little unconventional to update totalScore at the top of your do loop. Normally you would do that at the bottom of the loop (line 78). i.e. when the round is over. Consider if you wanted to display at line 83:
 
  cout << "Thanks for playing.  Your score was " << totalScore << endl;

totalScore would be wrong because you had not updated it yet. It is best practice to update things when they change rather than waiting to do it at the top of the loop.


I got it all to work
Topic archived. No new replies allowed.