Commit 0412d59c authored by Karine's avatar Karine

exercise_06_39, version4

parent 50ff58b6
......@@ -2,21 +2,17 @@
#include <cstdlib>
#include <ctime>
#include <unistd.h>
#include <cstring>
std::string
void
responseForGuessCount(const int guessCount)
{
std::string response;
if (guessCount <= 10) {
response = "Either you know the secret or you got lucky !";
if (guessCount < 10) {
std::cout << "Either you know the secret or you got lucky !" << std::endl;
} else if (10 == guessCount) {
response = "A hah! You know the secret!";
std::cout << "A hah! You know the secret!" << std::endl;
} else {
response = "You should be able to do better!";
std::cout << "You should be able to do better!" << std::endl;
}
return response;
}
int
......@@ -29,7 +25,7 @@ main()
const int numberToBeGuessed = 1 + std::rand() % 1000;
int guessCount = 0;
while(true) {
while (true) {
if (0 == guessCount) {
std::cout << "I have a number between 1 and 1000.\n"
<< "Can you guess my number?\n"
......@@ -39,24 +35,21 @@ main()
int answer;
std::cin >> answer;
++guessCount;
if (answer < numberToBeGuessed) {
std::cout << "Too low.Try again." << std::endl;
++guessCount;
continue;
} else if (answer > numberToBeGuessed) {
std::cout << "Too high.Try again." << std::endl;
++guessCount;
continue;
} else {
std::cout << "Excellent! You guessed the number!\n"
<< responseForGuessCount(guessCount)
<< "Would you like to play again (y or n)?" << std::endl;
std::cout << "Excellent! You guessed the number!\n";
responseForGuessCount(guessCount);
std::cout << "Would you like to play again (y or n)?" << std::endl;
char answerToQuit;
std::cin >> answerToQuit;
if ('y' == answerToQuit || 'Y' == answerToQuit) {
guessCount = 0;
continue;
} else if ('n' == answerToQuit || 'N' == answerToQuit) {
break;
}
......
......@@ -10,4 +10,5 @@ Too low.Try again.
Too low.Try again.
Too high.Try again.
Excellent! You guessed the number!
Either you know the secret or you got lucky !Would you like to play again (y or n)?
Either you know the secret or you got lucky !
Would you like to play again (y or n)?
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