Guessing Game

The user is required to guess a computer-generated number between 1 and 100 inclusive.
User will be told to give a higher or a lower number until the correct number is hit. Display
extra comment such as “You are fantastic!” if the user can hit the number within 3 guesses,
or display “You need more practice!” if the user cannot get the number within 8 guesses.


There is a problem where when the Guess#8 if the user still unable to guess on the 8th tries then the program will promt "You need more practice!".

What should i correct within my coding?

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
  #include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(void)
{
	srand((unsigned int)time(NULL)); // To not have the same numbers over and over again.

	while (true)
	{ // Main loop.
				   // Initialize and allocate.
		int number = rand() % 100 + 1; // System number is stored in here.
		int guess; // User guess is stored in here.
		int tries = 0; // Number of tries is stored here.

		cout << "\tWelcome to GUESSING GAME!\n";
		cout << "Please enter a number between 1 and 100. Are you up to the challenge?\n";
		cout << "-----------------------GUESSING GAME----------------------------------" << endl;
		cout << endl;
		while (true)
		{ // Get user number loop.
					   // Get number.
			cout << "Guess #" << tries + 1 << ":";
			cin >> guess;
			cin.ignore();

			// Check number.
			if (guess < number)
			{
				cout << "No, give a higher number.\n";
				cout << endl;
			}
			else if (guess > number) {
				cout << "No, give a lower number.\n";
				cout << endl;
			}
			else if (guess == number)
			{
				cout << "Yes, you are right!" << endl;
				break;
			}

			// If not number, increment tries.
			tries++;
		}

		// Check for tries.
		if (tries == 8)
		{
			cout << "You need more practice !\n";
			cout << "The correct answer is : " << number << endl;
			break;
		}
		else if (tries == 3)
		{
			cout << "Yes, you are right!";
			cout << "You are fantastic!\n";
			break;
		}

		return 0;
	}
	
}
Last edited on
maybe put
1
2
3
4
5
6
7
8
if( tries == 8 )
{
    //codes
}
else if( tries == 3 )
{
    //codes
}

inside the actual while ? because that will solve the problem you have. And what happens if the user guesses it on the first try ? or the second try ? or the fourth try ? you get my point by now because you only check 3rd try and 8th try
@Flaze07

for the case should i use and with the conditions?
something like this


1
2
3
4
5
6
7
8
9
10
11
12
13
if (tries <= 8 && guess != numrand)
			{
				cout << "You need more practice !\n";
				cout << "The correct answer is : " << numrand << endl;
				break;
			}

			else if (tries <= 4 && guess == numrand)
			{
				cout << "Yes, you are right!";
				cout << "You are fantastic!\n";
				break;
			}
Last edited on
Topic archived. No new replies allowed.