-
Notifications
You must be signed in to change notification settings - Fork 0
/
P70_3.cpp
56 lines (39 loc) · 1.39 KB
/
P70_3.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
52
53
54
55
56
// Guess My Number v2.0
// The new and improved number guessing game
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0)); // seed random number generator
int number;
int cpuGuess;
int countGuesses = 1; //Initialize countGuesses to 1 because computer has to make at least one guess
cout << "\tWelcome to Guess My Number v2.0!\n\n";
//Get number from user
cout << "Enter a number 1-100 you want the computer to try and guess:" << endl;
cin >> number;
//Generate random CPU guess from 1-100
cpuGuess = rand() % 100 + 1;
//When the computer isn't right, print either high or low and generate new random number
while (number != cpuGuess)
{
if (cpuGuess > number)
{
cout << "The computer guessed " << cpuGuess << " which was too high! Trying again..." << endl;
}
else
{
cout << "The computer guessed " << cpuGuess << " which was too low! Trying again..." << endl;
}
cpuGuess = rand() % 100 + 1;
countGuesses++;
}
//When the computer is finally right, print the original number and the number of tries it took the computer to find it
cout << "The computer has found that your number was " << number << " in " << countGuesses << " guesses";
//Spengler pause
char dum;
std::cin >> dum;
return 0;
}