-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguess.cpp
More file actions
32 lines (24 loc) · 725 Bytes
/
guess.cpp
File metadata and controls
32 lines (24 loc) · 725 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
32
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int num, guess, nguess = 0;
srand(time(0));
num = rand() % 100; // generates a number from 0 to 99
// Uncomment the next line to see the generated number for debugging
// cout << "The number is: " << num << endl;
do {
cout << "Guess the number: ";
cin >> guess;
if (guess > num) {
cout << "Lower number please\n\n";
} else if (guess < num) {
cout << "Higher number please\n\n";
} else {
cout << "You guessed it right in " << nguess + 1 << " attempts\n";
}
nguess++;
} while (guess != num);
return 0;
}