rng guessing code, error

Its the rng guessing game. When I run the code it seems like work properly.
However, It just ends some times when i put the number below the rng created.
for example, if the rng number was 35, when I put 34, it just ends. Please help me

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

// Program starts here:
int main()
{

const int MIN_VALUE = 1;
    const int MAX_VALUE = 100;
	int num1, num2, counter = 1;		//counter = 1 because you always need to try at least once to get the correct number.
    
	
	srand(time(NULL));														//change the random number everytime it runs
	num1 = rand() % (MAX_VALUE - MIN_VALUE + 1) + MIN_VALUE;				//num 1 is random number between 1 to 100
	
	cout << num1;
	//do-while loop used to intruct user to put correct number
    do {
        cout << "Number should be between 1 to 100" << endl;
        cout << "input your number" << endl;
        cin >> num2;
    } while (num2 < 1 || num2 > 100);
	
	
	//When user did correct guess prints out good job and shows the number of guess
	
	if(num2==num1){
	    cout << "Good Job" << endl;
		cout << "trials:" << counter << endl;
	}
	
	// two while loop giving clues to the user.
	// every time user enters the while loop, counter goes up
	
	else {
    while (num1>num2){
        cout << "Too low, try again." << endl;
        cin >> num2;
        counter++;
    }
    while (num1<num2){
        cout << "Too high, try again." << endl;
        cin >> num2;
        counter++;
    }
    }
	return 0; 
}
The reason is because you have faulty logic. Think about what the loops do.

1
2
3
4
5
    while (num1>num2){
        cout << "Too low, try again." << endl;
        cin >> num2;
        counter++;
    }

The loop checks if your guess is too low. What happens if your guess is too high? It skips this loop.

1
2
3
4
5
    while (num1<num2){
        cout << "Too high, try again." << endl;
        cin >> num2;
        counter++;
    }

This loop was after your other loop. It checks if your guess was too high. What happens if your guess is too low? It skips this loop. Does it go back to the loop before it to check if the number is too low? No, it doesn't.
I got what you are saying, but how can i fix it??
Those two loops should not be loops. Change them to if statements. Then put both if statements inside a single loop that will run until the user guesses the answer.

It will run at that point, but obviously you will want to move the if(num2==num1) stuff to after the new loop.
it goes forever
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

// Program starts here:
int main()
{

	// YOUR CODE GOES HERE

    const int min = 1;
    const int max = 100;
    int rn, un, counter = 1;
    
    srand(time(NULL));
    rn = rand () % (max - min + 1) + min;
    

    do{
    cout << "please guess the random number (1-100)" << endl;
    cin >> un;    
    }while (un<1 || un>100); 
    
    while(un!=rn){
        if (un > rn) {
            cout << "small" << endl;
            cin >> un;
            counter++;
        }
        else if (un > rn) {
            cout << "big" << endl;
            cin >> un;
            counter++;
        }   
    }
    
    
    cout << counter;
    cout << rn;
    cout << un;
	// End of program
	return 0; 
}
Last edited on
Check your conditions on lines 26 and 31.
thank you so much for the help :D
its works
Last edited on
It runs forever because you duplicated the conditions on the lines I mentioned.

What happens if you guess too small? You don't have anything to handle that. You don't ask for a new un so it's always un!=rn, giving you the infinite loop.
Bro. He said line 26 and 31. So look at line 26 and 31.

1
2
 if (un > rn) {
 cout << "small" << endl;



1
2
 else if (un > rn) {
      cout << "big" << endl;


Why are they identical? That makes no sense. Change the first one to un < rn
I got it :D thanks guys
Topic archived. No new replies allowed.