Commit 5221522b authored by Artak Yenokyan's avatar Artak Yenokyan

Merge branch 'exe_39' into 'master'

exercise_06_39, version1

See merge request bpcarm/trainings2017!1347
parents 3ac70b7b 0412d59c
progname = guessTheNumber
CC = g++
CFLAGS = -Wall -Wextra -Werror -g3
$(progname): $(progname).o | .gitignore
$(CC) $(CFLAGS) $^ -o $@
%.ii: %.cpp
$(CC) -E $(CFLAGS) $< -o $@
%.s: %.ii
$(CC) -S $(CFLAGS) $< -o $@
%.o: %.s
$(CC) -c $(CFLAGS) $< -o $@
.gitignore:
echo $(progname) > $@
clean:
rm -f *.ii *.s *.o *.out $(progname) .gitignore
test: $(progname)
./$(progname) < $@.in > $@.out
diff $@.out $@.expected && echo PASSED || echo FAILED
.PRECIOUS: $(progname).ii $(progname).s
.SECONDARY: $(progname).ii $(progname).s
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
void
responseForGuessCount(const int guessCount)
{
if (guessCount < 10) {
std::cout << "Either you know the secret or you got lucky !" << std::endl;
} else if (10 == guessCount) {
std::cout << "A hah! You know the secret!" << std::endl;
} else {
std::cout << "You should be able to do better!" << std::endl;
}
}
int
main()
{
if (::isatty(STDIN_FILENO)) {
std::srand(std::time(0));
}
const int numberToBeGuessed = 1 + std::rand() % 1000;
int guessCount = 0;
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;
++guessCount;
if (answer < numberToBeGuessed) {
std::cout << "Too low.Try again." << std::endl;
} else if (answer > numberToBeGuessed) {
std::cout << "Too high.Try again." << std::endl;
} else {
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;
} else if ('n' == answerToQuit || 'N' == answerToQuit) {
break;
}
}
}
return 0;
}
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
Too low.Try again.
Too low.Try again.
Too high.Try again.
Too high.Try again.
Too high.Try again.
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)?
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