number guessing game help

when I run my code it wont show the higher or lower part after 2 guesses any help would be appreciated, if I can figure out the first loop I will be able to finish the rest of the loops.

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
  Put the code you need help with here.
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <string>
#include <limits>

using namespace std;
void flush();

/*pick a number game where the user gets 4 chances to get the number right and 
 * the user gets 3 different levels to chose from.
 * 
 */
int main(int argc, char** argv) { // function type , arguments,/
    int number; // store assigned number
    int guess; // store guess
    int guess_lim = 4; // limits number of guess
    int (rand() % 10) + 1; // number generator
    string answer;
    bool go = true;
    int guess_count = 1; //to keep count of guess




    cout << " Welcome we are going to play a number guessing game today!!! \n"
            " you pick what level of difficulty you want between 1 and 3. \n"
            " 1 being the easiest and 3 the hardest!! \n\n"
            " You have four chances to get this right! \n";
    cin >> number;
    flush();
    if (number < 1 || number > 3) { // to make sure proper choices are being used from user or it will go back and ask again and to assign difficulty level/
        go = false;
        cout << "Thats is not an offered option\n ";
    }


    srand(time(NULL));
    do {

        switch (number) {


            case 1:do {
                    cout << "Guess a number between 1-10\n\n";
                    number = (rand() % 10) + 1; // to generate a random number between 1 and 10/
                    cin >> guess;
                    flush();

                    if (guess <= 10 && guess > 0) { // to make sure proper choices are being used from user or it will go back and ask again/
                        go = false;


                        if (guess > number) {
                            cout << "Too high!\n";
                            cin >> guess;
                            flush();
                            guess_count++;
                        }


                        if (guess < number) {
                            cout << "Too low!\n";
                            cin >> guess;
                            flush();
                            guess_count++;
                        }
                        else if (guess = number);{
                        cin >> guess;
                        flush();
                        cout << "\nCorrect! You got it in " << guess_count << " guesses!\n";//to show how many guesses it took 
                        cin >> guess_count;
                        flush();}


                        if (guess_count = 4);
                        { //  if guess count gets to 4 ends program
                            cout << "you lose out of turns!\n";
                        }
                        
                        return 0;
                    }
                } while (go);
I tried running your program from the code you provided. There were some syntax errors. Also what do you want your flush function to do? Here are some minor corrections to your code that i managed to do.
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
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <string>
#include <limits>

using namespace std;
void flush();

/*pick a number game where the user gets 4 chances to get the number right and 
 * the user gets 3 different levels to chose from.
 * 
 */
int main(int argc, char** argv) { // function type , arguments,/
    int number; // store assigned number
    int guess; // store guess
    int guess_lim = 4; // limits number of guess
    int (rand() % 10); // number generator
    string answer;
    bool go = true;
    int guess_count = 1; //to keep count of guess




    cout << " Welcome we are going to play a number guessing game today!!! \n"
            " you pick what level of difficulty you want between 1 and 3. \n"
            " 1 being the easiest and 3 the hardest!! \n\n"
            " You have four chances to get this right! \n";
    cin >> number;
    
    if (number < 1 || number > 3) { // to make sure proper choices are being used from user or it will go back and ask again and to assign difficulty level/
        go = false;
        cout << "Thats is not an offered option\n ";
    }


    srand(time(NULL));
    do {

        switch (number) {


            case 1:
				do {
                    cout << "Guess a number between 1-10\n\n";
                    number = (rand() % 10) + 1; // to generate a random number between 1 and 10/
                    cin >> guess;
                    flush();//

                    if (guess <= 10 && guess > 0)  // to make sure proper choices are being used from user or it will go back and ask again/
                        go = false;


                        if (guess > number) 
                            cout << "Too high!\n";
                            cin >> guess;
                            flush();
                            guess_count++;
                        


                        if (guess < number) 
                            cout << "Too low!\n";
                            cin >> guess;
                            flush();
                            guess_count++;
                        
                        if (guess = number)
                        cout << "\nCorrect! You got it in " << guess_count << " guesses!\n";//to show how many guesses it took (should only have this portion because you don't need to enter in a count or guess since you have gotten the number already)
                   


						if (guess_count = 4)
                        //  if guess count gets to 4 ends program
                            cout << "you lose out of turns!\n";
				
                     } while (!go)//needs ! because it will repeat if go is false 
		break;//assuming this is where you would end difficulty 1 so break would be needed around here
}

	}//will need another while statement
				
				return 0;//return needs to be down here because it will break out of your function before you get to your while statement
                    
                
		


for your false statements you can nest a do while or just a while statement here is an example for your code at line 33

1
2
3
4
5
cin>>number
while (while number<1 || number > 3)
{cout<<"Invalid Difficulty Chosen\n"<<"Please Choose A Value Between 1-3: ";
cin>>number;
}
Last edited on
Line 19 makes no sense and is redundant.
You never seed the random number generator.
If you did seed it, you'd be generating random numbers between guesses.
Your switch statement is unnecessarily hardcoded, but I can see where you were going with it.

Take a look at the number guessing game I slapped together:
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
#include <windows.h>
#include <iostream>
#include <string>
#include <ctime>

void flush();

const unsigned maxGuesses=4;

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

int main() {
	bool winFlag=false;
	unsigned input;
	std::cout << "We're going to play a number guessing game today!" << std::endl;
	std::cout << "You've only got four guesses!" << std::endl;
	std::cout << "Choose a difficulty setting (1-3): ";
	std::cin >> input;
	flush();
	unsigned max=input*10;
	int number=random(1, max);
	for(unsigned i=0; i<maxGuesses; i++) {
		std::cout << "I'm thinking of a number between 1-" << max << "." << std::endl;
		std::cout << "Guess: ";
		std::cin >> input;
		flush();
		if(input==number) {
			winFlag=true;
			break;
		}
		std::string output=(input<number)?"Low":"High";
		std::cout << "Too " << output << "!" << std::endl;
	}
	if(winFlag==true) {
		std::cout << "Nice job!" << std::endl;
	}else {
		std::cout << "Boohoo! Better luck next time." << std::endl;
	}
	std::cin.sync();
	std::cin.get();
	return 0;
}


Same functionality as yours, without the input checking.
thank you guys I am new at this and having a hell of a time with it is just part of the code that's giving me a hard time I will try to post up the whole code
this is using netbeans
Topic archived. No new replies allowed.