-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessTheNumber
39 lines (33 loc) · 883 Bytes
/
GuessTheNumber
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
srand(time(nullptr));
int numberToGuess = rand() % 100 + 1;
int playerGuess;
int guessCount = 0;
int guessCorrect = false;
std::cout << "Try and guess the number 1 through 100 that I am thinking of.." << std::endl;
std::cin >> playerGuess;
guessCount++;
while (guessCorrect == false) {
if (playerGuess == numberToGuess) {
std::cout << "You got it right! That took "
<< guessCount << " guesses." << std::endl;
guessCorrect = true;
}
else if (playerGuess > numberToGuess) {
std::cout << "Your guess is too high! Try again." << std::endl;
guessCount++;
std::cin >> playerGuess;
}
else {
std::cout << "Your guess is too low! Try again." << std::endl;
guessCount++;
std::cin >> playerGuess;
}
}
std::cout << "Thank you for playing!" << std::endl;
return 0;
}