-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguessTheNUmber2.cpp
More file actions
35 lines (28 loc) · 1.02 KB
/
guessTheNUmber2.cpp
File metadata and controls
35 lines (28 loc) · 1.02 KB
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
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand((unsigned int) time(NULL));
int numberToGuess = std::rand() % 100 + 1;
int playerGuess = 0;
int numberOfTries = 0;
cout << "Welcome to the Guess the Number Game!" << endl;
cout << "I've picked a number between 1 and 100." << endl;
cout << "Can you guess what it is?" << endl;
// Loop until the player guesses the correct number
do {
std::cout << "Enter your guess: ";
std::cin >> playerGuess;
numberOfTries++;
if (playerGuess > numberToGuess) {
std::cout << "Too high! Try again." << std::endl;
} else if (playerGuess < numberToGuess) {
std::cout << "Too low! Try again." << std::endl;
} else {
std::cout << "Congratulations! You've guessed the number "
<< numberToGuess << " in "
<< numberOfTries << " tries!" << std::endl;
}
} while (playerGuess != numberToGuess);
return 0;
}