#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
usingnamespace 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;
}
elseif(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;
}
elseif(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
.....
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
usingnamespace 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;
}
elseif(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;
}
elseif(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.
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.