-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab5_Q01.cpp
More file actions
32 lines (30 loc) · 856 Bytes
/
lab5_Q01.cpp
File metadata and controls
32 lines (30 loc) · 856 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
29
30
31
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void guess_number(int random, int turns) {
int num;
cout << "Enter your guess (between 1 and 100): ";
cin >> num;
if (num == random) {
cout << "YOU WIN!" << endl;
return;
}
if (turns <= 1) {
cout << "You lose! The number was " << random << endl;
return;
}
if (num < random) {
cout << "Too low You have " << turns - 1 << " turns left" << endl;
} else {
cout << "Too high You have " << turns - 1 << " turns left" << endl;
}
guess_number(random, turns - 1);
}
int main() {
srand(time(0));
int random = rand() % 100 + 1;
cout << "Welcome to the Number Guessing Game!" << endl;
cout << "You have 5 turns to guess the correct number" << endl;
guess_number(random, 5);
}