-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoWhileNum.cpp
More file actions
29 lines (25 loc) · 831 Bytes
/
Copy pathdoWhileNum.cpp
File metadata and controls
29 lines (25 loc) · 831 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
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main () {
int numberToGuess, numberGuessed, numberOfGuesses=0;
// initialize random number
srand(time(NULL));
numberToGuess = rand() % 10 + 1;
cout << "Guess a number between 1 and 10: ";
do {
cin >> numberGuessed;
numberOfGuesses++;
if (numberGuessed < numberToGuess) {
cout << "Higher. Try again: ";
} else if (numberGuessed > numberToGuess) {
cout << "Lower. Try again: ";
} else {
cout << "Congratulations! You guessed the number in " << numberOfGuesses << " tries.\n";
return 0;
}
} while (numberOfGuesses < 3);
cout << "Sorry, you've used all your tries. The number was " << numberToGuess << ".\n";
return 0;
}