-
Notifications
You must be signed in to change notification settings - Fork 0
/
mc8395_hw5_q2.cpp
51 lines (51 loc) · 1.68 KB
/
mc8395_hw5_q2.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
45
46
47
48
49
50
51
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int randomNum;
int minRange,maxRange;
int userGuess;
int remainingGuesses;
bool hasGuessedNum;
//seed random number
srand(time(0));
//create random number
randomNum = rand() % 100;
cout<<"I thought of a number between 1 and 100! Try to guess it."<<endl;
minRange = 1;
maxRange = 100;
remainingGuesses = 5;
hasGuessedNum = false;
while(remainingGuesses > 0 && hasGuessedNum == false) {
cout << "Range:[" << minRange << "-" << maxRange << "], Number of guesses left: " << remainingGuesses<< endl;
cout << "Your guess: ";
cin >> userGuess;
if (remainingGuesses > 1) {
if (userGuess < randomNum) {
minRange = userGuess + 1;
cout << "Wrong! My number is bigger."<<endl;
remainingGuesses--;
cout<<"\n"
} else if (userGuess > randomNum) {
maxRange = userGuess - 1 ;
cout << "Wrong! My number is smaller." << endl;
remainingGuesses--;
cout<<"\n"
} else if (userGuess == randomNum) {
cout << "Congrats! You guessed my number in " << remainingGuesses << " guesses!";
hasGuessedNum = true;
}
}
else{
if (userGuess == randomNum) {
cout << "Congrats you guessed my number in " << remainingGuesses << " guesses!";
hasGuessedNum = true;
}
else {
cout << "Out of guesses! My number is " << randomNum;
hasGuessedNum = true;
}
}
}
}