-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-guessing-game.cpp
51 lines (40 loc) · 1.15 KB
/
number-guessing-game.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;
#include <cstdlib>
#include <ctime>
#include <string>
int main()
{
// Random number generation
srand(time(0));
// Generate a random number between 1 and 100
int secretNumber = rand() % 100 + 1;
int guess;
int trials = 0;
string user;
cout << "\n*****Number Guessing Game!*****\n";
cout << "\nPlease enter your name: ";
getline(cin, user);
cout << "\nWelcome " << user << " to the Number Guessing Game!\n";
cout << "\nTry to guess the number between 1 and 100.\n\n";
do
{
cout << "\nEnter your guess: ";
cin >> guess;
trials++;
// Check if the guess is correct or not.
if (guess == secretNumber)
{
cout << "\nCongratulations! " << user << ", You guessed the correct number in " << trials << " attempts.\n\n";
}
else if (guess < secretNumber)
{
cout << "\nToo low! Try again.\n";
}
else
{
cout << "\nToo high! Try again.\n";
}
} while (guess != secretNumber);
return 0;
}