WIll rate life saver. Will compile on visual studios 2010 Each funcion must have
ID: 3623568 • Letter: W
Question
WIll rate life saver. Will compile on visual studios 2010
Each funcion must have it's own function heading and prototype. Then there will be a function call in main. The idea is the program is suppose to be structured so that if you need to change one function it will be easier to do. Thanks.
Write a program that plays a number guessing game with the user. A sample run of the game program would be as follows(user input is in bold face:
Welcome to that game of guess it!
I will choose a number between 1 and 100.
You will try to guess that number. If you guess wrong I will tell you if you guessed to high or to low.
You will have 6 tries to guess the number.
OK, I am thinking of a number. Try to guess it.
Your Guess? 50
Too high!
Your guess? 12
Too low!
Your guess? 112
Illegal guess. Your guess must be between 1 and 100.
Try again. Your guess? -20
Illegal guess. Your guess must be between 1 and 100.
Try again. Your guess? 23
***Correct*****
Want to play again? y
Ok, I am thinking of a number. Try to guess it.
Your guess? 85
Too high!
Your guess? 12
Too low!
Your guess? 57
***Correct*****
Want to play again? N
Goodbye, it was fun.
Hope to play Guess it with you again soon.
....................................................................................................................................
Hint: Look up the library functions rand() and srand(). Use them to establish the number to be guessed so that the number will be different each time the game is played.
Explanation / Answer
please rate - thanks
#include <iostream>
#include<ctime>
#include <cstdlib>
using namespace std;
void directions();
int think();
int getGuess();
bool checkGuess(int,int);
bool playagain();
void lostmessage(int);
void exit();
int main()
{srand(time(0));
int guess,num,tries;
bool good,again;
directions();
do
{num=think();
tries=0;
good=false;
again=false;
do
{
guess=getGuess();
good=checkGuess(num,guess);
tries++;
}while(!good&&tries<6);
if(!good&&tries==6)
lostmessage(num);
again=playagain();
}while(again);
exit();
return 0;
}
void lostmessage(int n)
{cout<<" Sorry :( You did't get it in 6 tries The number is "<<n<<endl;
}
bool playagain()
{char yorn;
cout<<"Want to play again? ";
cin>>yorn;
if(yorn=='y'||yorn=='Y')
return true;
return false;
}
void exit()
{cout<<"Goodbye, it was fun. ";
cout<<"Hope to play Guess it with you again soon. ";
}
void directions()
{cout<<"Welcome to that game of guess it! ";
cout<<"I will choose a number between 1 and 100. ";
cout<<"You will try to guess that number. If you guess wrong I will tell you if you ";
cout<<"guessed to high or to low. ";
cout<<"You will have 6 tries to guess the number. ";
}
int think()
{int num=1+(rand()%100);
cout<<"OK, I am thinking of a number. Try to guess it. ";
return num;
}
int getGuess()
{int guess;
cout<<"Your guess? ";
cin>>guess;
while(guess<1||guess>100)
{cout<<"Illegal guess. Your guess must be between 1 and 100. ";
cout<<"Try again. Your guess? ";
cin>>guess;
}
return guess;
}
bool checkGuess(int n,int g)
{if(n==g)
{cout<<"***Correct*** ";
return true;
}
else if(n>g)
cout<<"Too low! ";
else
cout<<"Too high! ";
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.