-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessing_game.cpp
44 lines (35 loc) · 1000 Bytes
/
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
// Author: codesoft vitual internship
// language used : c++
// Title: Guess the number
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(NULL));
int Hiddenum = (rand() % 50) + 1; // Generate a random number between 1 and 50.
int guess;
int attempts = 0;
cout << "Welcome to the Number Guessing Game!" << endl;
cout << "Try to guess the secret number between 1 and 50." << endl;
do
{
cout << "Enter your guess: ";
cin >> guess;
attempts++;
if (guess < Hiddenum)
{
cout << "Too low! Try again." << endl;
}
else if (guess > Hiddenum)
{
cout << "Too high! Try again." << endl;
}
else
{
cout << "Congratulations! You guessed the hidden number in " << attempts << " attempts." << endl;
}
} while (guess != Hiddenum);
return 0;
}