Commit 50ff58b6 authored by Karine's avatar Karine

exercise_06_39

parent 5aa93e2b
......@@ -2,30 +2,20 @@
#include <cstdlib>
#include <ctime>
#include <unistd.h>
bool
checkAnswer(const int answer, const int numberToBeGuessed)
{
if(answer == numberToBeGuessed) {
return true;
}
return false;
}
#include <cstring>
std::string
numberOfGuesses(const int number)
responseForGuessCount(const int guessCount)
{
std::string response;
if(number < 10) {
if (guessCount <= 10) {
response = "Either you know the secret or you got lucky !";
} else if(10 == number) {
response = "A hah! You know the secret!";
} else if (10 == guessCount) {
response = "A hah! You know the secret!";
} else {
response = "You should be able to do better!";
}
return response;
}
......@@ -37,42 +27,42 @@ main()
}
const int numberToBeGuessed = 1 + std::rand() % 1000;
std::cout << "I have a number between 1 and 1000.\n"
<< "Can you guess my number?\n"
<< "Please type your first guess." << std::endl;
int guessCount = 0;
bool test = true;
while(test) {
while(true) {
if (0 == guessCount) {
std::cout << "I have a number between 1 and 1000.\n"
<< "Can you guess my number?\n"
<< "Please type your first guess." << std::endl;
}
int answer;
std::cin >> answer;
if(answer < numberToBeGuessed) {
if (answer < numberToBeGuessed) {
std::cout << "Too low.Try again." << std::endl;
++guessCount;
continue;
} else if(answer > numberToBeGuessed) {
} else if (answer > numberToBeGuessed) {
std::cout << "Too high.Try again." << std::endl;
++guessCount;
continue;
} else {
test = false;
std::cout << "Excellent! You guessed the number!\n"
<< numberOfGuesses(guessCount)
<< "Would you like to play again (y or n)?" << std::endl;
char answerToQuit;
std::cin >> answerToQuit;
std::cout << "Excellent! You guessed the number!\n"
<< responseForGuessCount(guessCount)
<< "Would you like to play again (y or n)?" << std::endl;
char answerToQuit;
std::cin >> answerToQuit;
if('y' == answerToQuit) {
main();
}
else if('n' == answerToQuit) {
return 0;
}
if ('y' == answerToQuit || 'Y' == answerToQuit) {
guessCount = 0;
continue;
} else if ('n' == answerToQuit || 'N' == answerToQuit) {
break;
}
}
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment