New here!! Another number guessing game ?

Pages: 12
Hey guys and gals!!!!! Got a little something that I need a few answers to and help with this 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  {
    
    bool goOn=true;
    while(goOn)
    {
        srand(time(0));
        int easynumber = rand()%100+1;;
        int mediumnumber = rand()%1000+1;;
        int hardnumber = rand()%10000+1;;
        int impossiblenumber = rand()%100000+1;;
        int guess;
        int numberOFguesses = 0;
        int easylife = 50;
        int mediumlife = 20;
        int hardlife = 10;
        int impossiblelife = 4;
        int difficulty;
        
        
        
        char answer;
        
        cout << "Let's play a number guessing game!!\n";
        cout << "1 = Easy\n";
        cout << "2 = Medium\n";
        cout << "3 = Hard\n";
        cout << "4 = Impossible\n";
        cout << "Please select the difficulty level: ";
        cin >> difficulty;
        
        if(difficulty == 1)
        
        {
        
        cout << "Im thinking of a number between 1-100. Take a guess: ";
        cin >> guess;
        
        
        while(guess != easynumber)
        {
            if(guess < easynumber)
            {
                cout << "Your choice is a little too low, guess again: ";
                cin >> guess;
                numberOFguesses++;
                easylife--;
            }
            if(guess > easynumber)
            {
                cout << "Your choice is a little too high, guess again: ";
                cin >> guess;
                numberOFguesses++;
                easylife--;
            }
            
        if(guess == easynumber)
        {
            numberOFguesses++;
            cout << "Congrats!! You got it in " << numberOFguesses << " tries!!" << endl;
         
        }
            
        if(difficulty == 2)
            
        {
            
            cout << "Im thinking of a number between 1-1000. Take a guess: ";
            cin >> guess;
            
            
            while(guess != mediumnumber)
            {
                if(guess < mediumnumber)
                {
                    cout << "Your choice is a little too low, guess again: ";
                    cin >> guess;
                    numberOFguesses++;
                    mediumlife--;
                }
                if(guess > mediumnumber)
                {
                    cout << "Your choice is a little too high, guess again: ";
                    cin >> guess;
                    numberOFguesses++;
                    mediumlife--;
                }
              
            if(guess == mediumnumber)
            { 
                cout << "Congrats!! You got it in " << numberOFguesses << " tries!!" << endl;
                numberOFguesses++;
            }
            
            if(difficulty == 3)
                
            {
                
                cout << "Im thinking of a number between 1-100. Take a guess: ";
                cin >> guess;
                
                
                while(guess != hardnumber)
                {
                    if(guess < hardnumber)
                    {
                        cout << "Your choice is a little too low, guess again: ";
                        cin >> guess;
                        numberOFguesses++;
                        hardlife--;
                    }
                    if(guess > hardnumber)
                    {
                        cout << "Your choice is a little too high, guess again: ";
                        cin >> guess;
                        numberOFguesses++;
                        hardlife--;
                    }
                    
                }
                if(guess == hardnumber)
                { 
                    cout << "Congrats!! You got it in " << numberOFguesses << " tries!!" << endl;
                    numberOFguesses++;
                }
                
                else
                    
                {
                    
                    cout << "Im thinking of a number between 1-100. Take a guess: ";
                    cin >> guess;
                    
                    
                    while(guess != impossiblenumber)
                    {
                        if(guess < impossiblenumber)
                        {
                            cout << "Your choice is a little too low, guess again: ";
                            cin >> guess;
                            numberOFguesses++;
                            impossiblelife--;
                        }
                        if(guess > impossiblenumber)
                        {
                            cout << "Your choice is a little too high, guess again: ";
                            cin >> guess;
                            numberOFguesses++;
                            impossiblelife--;
                        }
                     
                    if(guess == impossiblenumber)
                    { 
                        cout << "OMG!!!!!! You actually got it in " << numberOFguesses << " tries!!" << endl;
                        numberOFguesses++;
                    }


            
            if(easylife == 0, mediumlife == 0, hardlife == 0, impossiblelife == 0)
            {
                cout << "_____________" << endl;
                cout << "| Game Over |" << endl;
                cout << "_____________" << endl;
            }
            
            }
            cout << "would you like to play again? Enter y or n: ";
            cin >> answer;
            if(answer != 'y')
            {
                goOn = false;
                cout << "thanks for playing!" << endl;
        }
    }
            
    return 0;
}


welp.... there it is in all it's ?glory? lol. Well, like many on this forum I am very very new to programming and thought I would try my hand in making one of these infamous number guessing games. Some of you skilled programmers may be able to look at this and say it's crap or can spot the bugs quickly. Well, if not I will try and explain what is going on here.

1) I wanted there to be different difficulty levels
2) I wanted each difficulty level to have a certain amount of attempts
3) I want it to count the number of tries for each one
4) I would like for it to say different things depending on how far away from the number you are, example if the number is 50 and the guess is 49 I would like for it to say "You are very close" but if they choose something like 2 I would like it to say "You are way too low" <-----This one really has me stumped.
5) I'm not sure whats going on with this program but it will only let me choose option #1 for the difficulty level????
6) The program completely ignores the loop at the end asking if the player would like to play again......?

Thank you guys so much for your help in advance.
1.) You can have multiple difficulty levels with only one variable if you want. You can use a switch case or an if else if structure to set the maximum value based on what level they want.
2.) Same as above but with attempts rather than maximum value.
3.) You can keep track of tries by subracting the remaining attempts from maximum number of attempts.
4.) Set some variables that are the range for very close and far away, and if their guess is less than or equal to that number, you output a message based on that range's meaning.
5.) Your while loop on line 39 has an ending brace in the wrong place.
6.) Not sure what your problem is right now. Your code is very long. Try answer = tolower(answer); //if answer is 'Y' it's now 'y'

Try these fixes and put in your code. If you have any questions, feel free to ask.

Hope this helps.
Basically for number 4 you want to do something like

1
2
3
4
5
6
7
int guess_distance = answer - guess;
if( guess_distance < 0 ) guess_distance = - guess_distance; //if guess was higher than the answer

if( guess_distance < 5 ) std::cout << "Amazing!" << std::endl;
else if( guess_distance < 10 ) std::cout << "Nice guess!" << std::endl;
else if( guess_distance < 20 ) std::cout << "Getting there." << std::endl;
else std::cout << "You have a ways to go." << std::endl;
I finished writing it. If you want any help, then I have a working version that mostly follows what you want.

Some of what you wanted I was too lazy to do, but I have essentially the same thing you want.

@giblit
I did the same thing, but it was a little more variable, and I used abs(num - guess); where num is the set number and guess is what the user is guessing.
well abs() is the same thing I did by negating it if it was a negative value.
It is. I just wanted to give the OP another option. I didn't mean to call you out.
Last edited on
It's no problem I didn't feel called out. Also I just realized OP you have if 1...if 2... if 3... Have you considered a switch then a default answer/guess?

example of what I mean

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
const int easy_guesses = 10;
const int medium_guesses = 5;
const int hard_guesses = 3;
const int easy_answer = rand() % 100 + 1;
const int medium_answer = rand() % 1000 + 1;
const int hard_answer = rand() % 10000 + 1;

int answer = 0;
int GUESSES = 0;
int guess = 0;
int mode = 0;

std::cout << "Would you like to play Easy(1) , Medium(2) , or Hard(3): ";
std::cin >> mode;

switch( mode )
{
    case 1:
        answer = easy_answer;
        GUESSES = easy_guesses;
        break;
    case 2:
        answer = medium_answer;
        GUESSES = medium_guesses;
    //....
}



Another method would be to use an array too something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const int DIFFICULTIES = 3;
int answers[DIFFICULTIES];
int guesses[DIFFICULTIES];
int mode = 0;

for( int i = 0; i < DIFFICULTIES; ++i )
{
    answers[i] = rand() 10*( ( i + 1 ) * 10 );
    guesses[i] = 10 - ( i * 3 );
}


std::cout << "Which difficulty Easy(1) , Medium(2) , or Hard(3): ";
std::cin >> mode;

for( int i = 0; i < guesses[mode]; ++i )
{
    //stuff
    if( guess == answer[mode] ) break;
    //stuff
}


Just to give you a few ideas.

*edit on line 7 it should be:
rand() % ( 10 * ( ( i + 1 ) * 10 ) ) + 1;

Reason being:

( 10 * ( ( 0 + 1 ) * 10 ) ) = ( 10 * ( 1 * 10 ) ) = ( 10 * 10 ) = 100
( 10 * ( ( 1 + 1 ) * 10 ) ) = ( 10 * ( 2 * 10 ) ) = ( 10 * 20 ) = 200
( 10 * ( ( 2 + 1 ) * 10 ) ) = ( 10 * ( 3 * 10 ) ) = ( 10 * 30 ) = 300

I felt like incrementing each difficulty by 100% you could increment by more if you would like just modify the formula.
Last edited on
Another option is what I did in my 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
                int maxGuess = 1;
		int num;
		int guess = 0;
		int currentGuess;
		int smallMiss;
		int midMiss;
		int largeMiss;
		int level = 0;
		int remaining;
		int maxAttempts;

		while(level < 1 || level > 4)
		{
			cout << "Let's play a number guessing game!!\n";
			cout << "1 = Easy\n";
			cout << "2 = Medium\n";
			cout << "3 = Hard\n";
			cout << "4 = Impossible\n";
			cout << "Please select the difficulty level: ";

			cin >> level;

			switch(level)
			{
			case 4:
				maxGuess *= 10;
			case 3:
				maxGuess *= 10;
			case 2:
				maxGuess *= 10;
			case 1:
				maxGuess *= 100;
			}

			if(level == 1)
				maxAttempts = 50;
			else if(level == 2)
				maxAttempts = 20;
			else if(level == 3)
				maxAttempts = 10;
			else
				maxAttempts = 4;
		}
Last edited on
Why so hardcoded?

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
#define NOMINMAX
#include <windows.h>
#include <iostream>
#include <ctime>
#include <limits>

typedef long long QWORD;//lies.

QWORD random(QWORD min, QWORD max) {
	static bool random=false;
	if(random==false) {
		random=true;
		srand(time(0));
	}
	return (min+(rand()%max+min+1));
}

int main(int argc, char* argv[]) {
	unsigned short difficulty = 0;
	while(true) {
		std::cout << "Enter difficulty:\t";
		if(std::cin >> difficulty) {
			break;
		}else {
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
	}
	QWORD min = 0, max = 10*difficulty;
	QWORD number = random(min, max);
	std::cout << "The secret number would have been " << number << " if I would have finished this, but I didn't because I'm lazy." << std::endl;
	std::cin.sync();//sorry.
	std::cin.get();
	return 0;
}
Last edited on
I ran this program and it messed up codeblocks. Anyone know how to reset it to default settings?
What do you mean messed up code::blocks? Also @xism why do you have lines 7 , 10 , 11 , 12, 13( in that function ), line 14 , and arguments if you're not using them? It seems you made it a lot harder than it needed to be.
Wow guys!!!! Thank you so much for all of this information. I thought about using the switch loops but heard they can be a bit buggy at times??? I know this must be frustrating, but some of this stuff is going over my head a little bit..... I still have so much to learn, and this program may be way above my skill level for now.

@xismn
- I honestly have no clue on the libraries you are using, as I am only using
#include <iostream>
using namespace std;

@GRex2595
-I will work with the switch loops and try and run them, but as stated above, I am not very familiar with them but seems easy enough.
I would just like to mention switches aren't loops they are pretty similar to if statements and they aren't buggy people just prefer if/else if besides for situations where you have a bunch of different options like 1-99 all do different things. It would be a lot more code to do a bunch of if/else vs case 1: case 2: case 3:...
Well, the program somehow opened all the #include files like iostream and they got messed up. I want to know how to restore the #include stuff to default settings. I already tried re-installing it. Thank you for your time.
Last edited on
@giblit
- LOL wow just shows how new I am to programming. I really do enjoy the challenge that it brings and I am always up for problem solving. I just hope that I will be able to make a career out of this one day....

Well Like I mentioned I was going to work on this code some more, well I have not messed with switches yet but I did write a code that works pretty well. The only thing with it is that once you run out of lives it does not ask you if you would like to play again....... has me stumped (BIG SURPRISE HERE!!! lol)

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
{
    
        bool playagain = true;
        while (playagain)
        {
    
    srand(time(0));
    int number = rand()%100+1;;
    int guess;
    int numberOFguesses = 0;
    int numberOFlives = 10;
    char answer;
    cout << "Im thinking of a number between 1-100.\n";
    cout << "Take a guess, but be careful, you only have " << numberOFlives << " lives\n";
    cout << "Your guess is: ";
    cin >> guess;
         //   numberOFlives = 9;

    while(guess != number )
    {
        if(guess < number && numberOFlives != 0)
        {
            numberOFlives--;
            cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
            cout << "Your choice is too low, guess again: ";
            cin >> guess;
            numberOFguesses++;
            
            
            if(numberOFlives == 0)
            {
                cout << "-------------" << endl;
                cout << "| GAME OVER |" << endl;
                cout << "-------------" << endl;
            }
        }
        if(guess > number && numberOFlives != 0)
        {
            numberOFlives--;
            cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
            cout << "Your choice is too high, guess again: ";
            cin >> guess;
            numberOFguesses++;
            
            
            if(numberOFlives == 0)
            {
                cout << "-------------" << endl;
                cout << "| GAME OVER |" << endl;
                cout << "-------------" << endl;
            }
        }

        if(guess == number && numberOFlives != 0)
        {
            numberOFguesses++;
            cout << "Congrats!! You got it in " << numberOFguesses << " tries!!" << endl;
            
        }
        
    }
            
            cout << "Would you like to play again? (y or n)" << endl;
            cin >> answer;
            if(answer != 'y')
            {
                playagain = false;
                cout << "Hope you had fun playing!\n";
            }
        }
}
It looks like it should. One thing I can mention though instead of checking the number of "lives" left in your if statements why not loop only when there are lives left? So you could remove that check from all the if statements ( they should be if/else if related also ) then on line 19 change to while( guess != answer && lives > 0 )

Then on the next line you want to decrement the lives so put --lives;

Edit also can you properly format it it is kind of hard to read. Basically inside each curly brace you want to indent each line.

So when you have a curly in a curly you indent twice

ex:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    //one curly
    if( stuff )
    {
        //two curly
        while( stuff )
        {
            //three curly
        }
    }
}


See how they all line up and it is easy to tell where each one ends.

versus how you have it

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    //one curly
    if( stuff )
    {
      //two curly
       while( stuff )
     {
           //three curly
         }
         }
}
A lot harder to read.

It's mainly with your first while I think you added it then forgot to indent afterwards so loops really weird from lines 60-70
Last edited on
WOW MAN!!!!! AHHH thank you so much!! you have been a huge help, I can't thank you enough. Here is my final (for now) project and it works beautifully!!!!
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
#include <iostream>
using namespace std;
int main()
{
    
        bool playagain = true;
        while (playagain)
        {
    
    srand(time(0));
    int number = rand()%100+1;;
    int guess;
    int numberOFguesses = 0;
    int numberOFlives = 10;
    char answer;
    cout << "Im thinking of a number between 1-100.\n";
    cout << "Take a guess, but be careful, you only have " << numberOFlives << " lives\n";
    cout << "Your guess is: ";
    cin >> guess;

    while(guess != number && numberOFlives > 0)
    {
        if(guess < number && numberOFlives != 0)
        {
            numberOFlives--;
            cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
            cout << "Your choice is too low, guess again: ";
            cin >> guess;
            numberOFguesses++;
            
            if(numberOFlives == 0)
            {
                cout << "-------------" << endl;
                cout << "| GAME OVER |" << endl;
                cout << "-------------" << endl;
            }
        }
        if(guess > number && numberOFlives != 0)
        {
            numberOFlives--;
            cout << "That cost you a life point, you only have " << numberOFlives << " lives left.\n";
            cout << "Your choice is too high, guess again: ";
            cin >> guess;
            numberOFguesses++;
            
            if(numberOFlives == 0)
            {
                cout << "-------------" << endl;
                cout << "| GAME OVER |" << endl;
                cout << "-------------" << endl;
            }
        }
        if(guess == number && numberOFlives != 0)
        {
            numberOFguesses++;
            cout << "Congrats!! You got it in " << numberOFguesses << " tries!!" << endl;
        }
        
    }
            
    cout << "Would you like to play again? (y or n)" << endl;
    cin >> answer;
    if(answer != 'y')
            {
                playagain = false;
                cout << "Hope you had fun playing!\n";
            }
        }
}


I tried cleaning it up as much as I could. But again THANK YOU so much. Now I will start working on how to get different answers like if you are 5 numbers away it should say "You are very close" and if you're 30 away it will say "You are so far off" and things like that. I have already tried this
1
2
3
4
5
6
7
8
9
10
if(guess <= number - 10)
{
      cout << "You are a little far off on the low side, guess again: ";
      cin >> guess;
}
if(guess >= number + 10)
{
      cout << "You are a little far off on the high side, guess again: ";
      cin >> guess;
}


and for some reason it does not work???
I would calculate the difference then depending on the difference output different things like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

int difference = 0;

//loop


difference = guess - number; //if guess is greater its even

if( difference < 0 && difference > -11 )
{
    std::cout << "You are a little too low" << std::endl;
}
else if( difference > 0 && difference < 11 )
{
    std::cout << "You are a little too high" << std::endl;
}


Another way might be like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int difference = 0;
bool to_low = true;

//loop

difference = guess - number;

to_low = difference < 0 ;
if( to_low ) difference = - difference; //absolute value

if( difference < 10 )
{
    std::cout << "You were a little off on the ";
}
else if( /*more conditions*/ ){}

if( to_low ) std::cout << "low side." << std::endl;
else std::cout << "high side." << std::endl;


*Moved the to low/too high out of the first difference check.

Last edited on
@giblit
@xism why do you have lines 7 , 10 , 11 , 12, 13( in that function ), line 14 , and arguments if you're not using them?

What do you mean?

EDIT* WHAT DO YOU MEAN???????????
Last edited on
Pages: 12