Simple solution, major frustration

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

int main(){
//Variable declaration
string name(" ");
int round, cntTries = 0;
int guessSelection = 0;
char again = 'y';
again = toupper(again);
//Random number generator
srand(time(0));
int high = 100;
int low = 0;
//Output + selection process
cout << "Guess the number game" << endl;
cout << "\nPlease enter your name" << endl;
getline(cin, name);
cout << "Hello " << name << endl;
cout << "Would you like me to guess your number or would you like to have a shot\n"
<< "at guessing my number?" << endl;
cout << "(For your number type '1' :: For my number type '2')" << endl;
cin >> guessSelection;
//Loops of selection
//Selection = 1
while(again=='y'){
    if(guessSelection == 1){
        int myNum = 0;
        int compGuess = 0;
        char answer = 'n';
        cout << "Please enter your number on a scale of 1-100 using integers only\n"
            << "No decimal values allowed (type -1 at anytime to exit)" << endl;
        cin >> myNum;
        compGuess=(1+rand()%100);
        if(myNum > 100 || myNum < 0){
            cout << "You have entered a value too high or too low.. Must be between 1 & 100" << endl;
        }
                while(answer == 'n' || answer == 'N' && compGuess != myNum){
                        cout << "Is this your number? " << compGuess << endl;
                        cin >> answer;
                        if(answer == 'n' || answer == 'N'){
                            if(compGuess > myNum){
                                cout << "Too high.. " << endl;
                                high = compGuess;
                                compGuess = rand() % (high - low + 1) - low;
                            }
                            else if(compGuess < myNum){
                                cout << "Too low.. " << endl;
                                low = compGuess;
                                rand()%(high - low + 1) + low;
                            }
                        }
                }
        }
//Selection = 2
    if(guessSelection == 2){
        int myGuess=0;
        int compNum=0;
        compNum=(1+rand()%100);
        cout << "You have chosen to guess my number.. Have at it!" << endl;
        cout << "Make a guess.. (1-100)" << endl;
        cin >> myGuess;
        while(myGuess!=compNum){
            cout << "Keep guessing.. " << endl;
            cin >> myGuess;
            if(myGuess == compNum){
                cout << "You guessed it!" << endl;
            }
            if(myGuess > compNum){
                cout << "Too high, try again." << endl;
            }
            else if(myGuess < compNum){
                cout << "Too low, try again." << endl;
            }
        }
    }
//Selection = something other than 1 or 2
    else{
    cout << "You've made an invalid selection.." << endl;
}
cout << "Would you like to play again? y or n" << endl;
cin >> again;
}
}


Whenever I run the program, it's fine until I get to the point where it asks which game they want to play. if I select 1, it'll ask for my number, if I give a number bigger than 30, the program quits. I didn't program it to do that. If I pick a number 30 or below, it'll run it fine, but if the number the computer guesses is too low, it'll repeat that same number over and over and not change. Also, if I select option 2, the program will quit.

example of 1st issue:
Select number: 25
is this your number?: 55
too high: 45
too high: 30
too high: 15
too low: 15
too low: 15
.....
Last edited on
Edited my code: It works, to an extent. I can't exit my loop with -1
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
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
using namespace std;

int main(){
//Variable declaration
string name(" ");
int round, cntTries = 0;
int guessSelection = 0;
char again = 'y';
again = toupper(again);
//Random number generator
srand(time(0));
int high = 100;
int low = 0;
//Output + selection process
cout << "Guess the number game" << endl;
cout << "\nPlease enter your name" << endl;
getline(cin, name);
cout << "Hello " << name << endl;
cout << "Would you like me to guess your number or would you like to have a shot\n"
<< "at guessing my number?" << endl;
cout << "(For your number type '1' :: For my number type '2')" << endl;
cin >> guessSelection;
//Loops of selection
//Selection = 1
do{
    if(guessSelection == 1){
        int myNum = 0;
        int compGuess = 0;
        char answer = 'n';
        cout << "Please enter your number on a scale of 1-100 using integers only\n"
            << "No decimal values allowed (type -1 to exit)" << endl;
        cin >> myNum;
        compGuess=(1+rand()%100);
        if(myNum > 100){
            cout << "You have entered a value too high.. Must be between 1 & 100" << endl;
        }
                while(answer == 'n' || answer == 'N' && compGuess != myNum){
                        cout << "Is this your number? " << compGuess << endl;
                        cin >> answer;
                        if(answer == 'n' || answer == 'N'){
                            if(compGuess > myNum){
                                cout << "Too high.. " << endl;
                                high = compGuess;
                                compGuess = rand() % (high - low + 1) + low;
                            }
                            else if(compGuess < myNum){
                                cout << "Too low.. " << endl;
                                low = compGuess;
                                compGuess = rand() % (high - low + 1) + low;
                            }
                        }
                }
        }
//Selection = 2
    if(guessSelection == 2){
        int myGuess=0;
        int compNum=0;
        compNum=(1+rand()%100);
        cout << "You have chosen to guess my number.. Have at it!" << endl;
        cout << "Make a guess.. (1-100)" << endl;
        cin >> myGuess;
        while(myGuess!=compNum){
            cout << "Keep guessing.. " << endl;
            cin >> myGuess;
            if(myGuess == compNum){
                cout << "You guessed it!" << endl;
            }
            if(myGuess > compNum){
                cout << "Too high, try again." << endl;
            }
            else if(myGuess < compNum){
                cout << "Too low, try again." << endl;
            }
        }
    }
//Selection = something other than 1 or 2
    else{
    cout << "You've made an invalid selection.." << endl;
}
cout << "Would you like to play again? y or n" << endl;
cin >> again;
}
while(again == 'y' || again == 'Y');
}
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
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
using namespace std;

int main(){
//Variable declaration
string name(" ");
int round, cntTries = 0;
int guessSelection = 0;
char again = 'y';
again = toupper(again);
//Random number generator
srand(time(0));
int high = 100;
int low = 0;
//Output + selection process
cout << "Guess the number game" << endl;
cout << "\nPlease enter your name" << endl;
getline(cin, name);
cout << "Hello " << name << endl;
cout << "Would you like me to guess your number or would you like to have a shot\n"
<< "at guessing my number?" << endl;
cout << "(For your number type '1' :: For my number type '2')" << endl;
cin >> guessSelection;
//Loops of selection
//Selection = 1

do{
    if(guessSelection == 1){
        int myNum = 0;
        int compGuess = 0;
        char answer = ' ';
        cout << "Please enter your number on a scale of 1-100 using integers only\n"
            << "No decimal values allowed (type -1 to exit)" << endl;
        cin >> myNum;
        compGuess=(1+rand()%100);
        if(myNum > 100){
            cout << "You have entered a value too high.. Must be between 1 & 100" << endl;
            cout << "Again?" << endl;
            cin >> again;
            }
                       cout << "Is this your number? " << compGuess << endl;
                        cin >> answer;
                while(answer == 'n' || answer == 'N' && compGuess != myNum){
                        cout << "What about this: " << compGuess << endl;
                        cin >> answer;
                            if(compGuess > myNum){
                                cout << "Too high.. " << endl;
                                high = compGuess;
                                compGuess = rand() % (high - low + 1) + low;
                            }
                            else if(compGuess < myNum){
                                cout << "Too low.. " << endl;
                                low = compGuess;
                                compGuess = rand() % (high - low + 1) + low;
                            }
                        }
                }

//Selection = 2
    if(guessSelection == 2){
        int myGuess=0;
        int compNum=0;
        compNum=(1+rand()%100);
        cout << "You have chosen to guess my number.. Have at it!" << endl;
        cout << "Make a guess.. (1-100)" << endl;
        cin >> myGuess;
        while(myGuess!=compNum){
            cout << "Keep guessing.. " << endl;
            cin >> myGuess;
            if(myGuess == compNum){
                cout << "You guessed it!" << endl;
            }
            if(myGuess > compNum){
                cout << "Too high, try again." << endl;
            }
            else if(myGuess < compNum){
                cout << "Too low, try again." << endl;
            }
        }
    }
//Selection = something other than 1 or 2
    if(guessSelection > 2){
    cout << "You've made an invalid selection.." << endl;
}
cout << "Would you like to play again? y or n" << endl;
cin >> again;
}
while(again == 'y');
}


Progressing slightly, but now I'm truly at a stalemate. I've spent ~ 35 - 40 min trying different solutions to my problem, and now I'm stuck.

I can't get myNum > 100 to actually work correctly, it'll pretend I entered a number and computer starts guessing.
I also can't get the computer to properly guess, say he guess 22 and it says too low, there's a chance he'll guess 22 again either later, or directly after guessing 22..

Please, any help.. at all.. Test the code out for yourself, and let me know what ya think, what a solution, anything. Not homework, just projects during class break.
bump..
Make it so you can't enter bellow 1 or above 100 so a glitch doesn't happen.
(This won't solve your problem, just make it more user friendly.)
Last edited on
Okay, I got it. It took awhile but it works. I tested it like 300 times. Here is the code: (the underlined stuff is what I added and edited.)

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

int used[100];

int main(){
//Variable declaration
string name(" ");
int round, cntTries = 0;
int guessSelection = 0;
char again = 'y';
again = toupper(again);
//Random number generator
srand(time(0));
int high = 100;
int low = 0;
//Output + selection process
cout << "Guess the number game" << endl;
cout << "\nPlease enter your name" << endl;
getline(cin, name);
cout << "Hello " << name << endl;
cout << "Would you like me to guess your number or would you like to have a shot\n"
<< "at guessing my number?" << endl;
cout << "(For your number type '1' :: For my number type '2')" << endl;
cin >> guessSelection;
//Loops of selection
//Selection = 1

do{
    if(guessSelection == 1){
        int myNum = 0;
        int compGuess = 0;
        char answer = ' ';
        cout << "Please enter your number on a scale of 1-100 using integers only\n"
            << "No decimal values allowed (type -1 to exit)" << endl;
        cin >> myNum;
        compGuess=(1+rand()%100);
        if(myNum > 100){
			///////////////////////////////////////////|
			//////////////////////What I added/eddited V

			for (int a=0; a>101; a++){
				used[a]=0;
			}
            cout << "You have entered a value too high.. Must be between 1 & 100" << endl;
            cout << "Again?" << endl;
            cin >> again;
            }
                answer = 'n';
				int number = 0;
				int clear = 1;
				int dc;
                while(answer == 'n' || answer == 'N' && compGuess != myNum){
					number++;
					while (clear == 0){
						dc = 1;
					for (int a=0; a<101; a++){
						if (compGuess == used[a]){
							if(compGuess > myNum){
                                compGuess = rand() % (high - low + 1) + low;
								dc = 5;
                            }
                            else if(compGuess < myNum){
                                compGuess = rand() % (high - low + 1) + low;
								dc = 5;
						}
					}
					}
					if (dc == 5){
						clear = 0;}
					else goto next;
					}
					next:
					clear = 0;
					used[number] = compGuess; 
                        cout << "What about this: " << compGuess << endl;
                        cin >> answer;
                            if(compGuess > myNum){
                                cout << "Too high.. " << endl;
                                high = compGuess;
                            }
                            else if(compGuess < myNum){
                                cout << "Too low.. " << endl;
                                low = compGuess;
                            }
							number++;
							used[number] = compGuess; 
                        }
                }
								///////////////////////////////////////////^
								//////////////////////What I added/eddited |
//Selection = 2
    if(guessSelection == 2){
        int myGuess=0;
        int compNum=0;
        compNum=(1+rand()%100);
		cout << endl<< compNum << endl;
        cout << "You have chosen to guess my number.. Have at it!" << endl;
        cout << "Make a guess.. (1-100)" << endl;
        cin >> myGuess;
        while(myGuess!=compNum){
            cout << "Keep guessing.. " << endl;
            cin >> myGuess;
            if(myGuess == compNum){
                cout << "You guessed it!" << endl;
            }
            if(myGuess > compNum){
                cout << "Too high, try again." << endl;
            }
            else if(myGuess < compNum){
                cout << "Too low, try again." << endl;
            }
        }
    }
//Selection = something other than 1 or 2
    if(guessSelection > 2){
    cout << "You've made an invalid selection.." << endl;
}
cout << "Would you like to play again? y or n" << endl;
cin >> again;
}
while(again == 'y');
}


Try it. I hope it works.
Last edited on
One error whit what I just posted. If the user enters the number '0' for the computer to guess it glitches. Make it so it says you can't enter bellow 1.
I'll try it out tonight! Thanks for the reply!
My only issue is.. When I enter a number greater than 100, it'll still continue on with the game as if I entered something below 100.
Just use if to tell it not to go above or bellow those numbers.
Topic archived. No new replies allowed.