-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.cpp
More file actions
28 lines (24 loc) · 780 Bytes
/
GuessingGame.cpp
File metadata and controls
28 lines (24 loc) · 780 Bytes
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
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main() {
int secretNumber, guess, attempts = 0;
srand(time(0));
secretNumber = rand() % 100 + 1;
cout<<"Welcome to the Guessing Game!\n"<<endl;
cout<<"Guess the number between 1 and 100:\n"<<endl;
do {
cout<<"Enter your guess: "<<endl;
cin>>guess;
attempts++;
if (guess > secretNumber) {
cout<<"Too high! Try again.\n"<<endl;
} else if (guess < secretNumber) {
cout<<"Too low! Try again.\n"<<endl;
} else {
cout<<"Congratulations! You guessed the number in " <<attempts++<< " attempts.\n"<<endl;
}
} while (guess != secretNumber);
return 0;
}