not sure why when i press n the function doesn\'t break, but if ipress y it work
ID: 3609344 • Letter: N
Question
not sure why when i press n the function doesn't break, but if ipress y it works going to the next function.this my code, if you can spot the problem it would be helpful byshowing the code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
/*
* Prototypes ---place the prototypes for your user definedfunctions here
*/
//free function....
char yesNo();
int getUserNumber();
int getRandomNumber();
int random_number;
int main(int argc, char *argv[]){
//your main will have a repeating loop that gets the user to playthe guessing game as many times as they like.
//there will be function calls to get the random number, and to getthe user to enter a number.
cout << " ***Guessing Game***" <<endl<<endl;
getUserNumber();
system("PAUSE");
}
//Down here you will define your 2 functions, more if you see otherparts suitable for functions
//This is your free function. It gets the Y or N characterfrom the user and returns it
char yesNo()
{
char result;
bool valid=false;
while (valid==false){
//user will be forced to entera letter until they give a valid one
cout<<"Do you want toplay again? (Y/y or N/n) ";
cin>>result;
//check to see if it is avalid choice
if ((result=='y') ||(result=='Y')){
cout<< endl; valid = true;
getUserNumber();}
else if ((result=='n') ||(result=='N')){
cout << endl <<endl << "Thanks for playing!"<<endl; break;}
else
cout<<"You have to type a valid character.... Tryagain"<<endl;
}//end while
//sends the valid character back
return result;
}//end yesNo
int getRandomNumber(){
const long MAX = 100, MIN = 1;
srand(time(NULL));
random_number = (rand() % (MAX - MIN + 1)) +MIN;
}
int getUserNumber(){
int num, guesses=1;
getRandomNumber();
while(num != random_number, guesses++){
cout<< "Enter an integer number (1-100): ";
cin >>num;
cout<<endl;
if (num <1 || num > 100){
cout << "The number must be from 1 to 100! Try again..."<<endl <<endl;}
else {
if (random_number > num)
cout << "Guess is too small!" <<endl <<endl;
else if (random_number < num)
cout << "Guess is too big!" <<endl <<endl;
else if (random_number = num){
cout << "You guessed it! It took you ";
guesses = guesses - 1;
cout << guesses;
cout << " guesses" <<endl <<endl;
yesNo();}
}}}
Explanation / Answer
please rate - thanks It works now, value from yesNo was never returned and checked and afew other minor changes. compare the 2 sets of code #include #include #include using namespace std; /* * Prototypes ---place the prototypes for your user definedfunctions here */ //free function.... bool yesNo(); void getUserNumber(); int getRandomNumber(); int random_number; int main(int argc, char *argv[]){ //your main will have a repeating loop that gets the user to playthe guessing game as many times as they like. //there will be function calls to get the random number, and to getthe user to enter a number. coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.