Jun 20, 2015 at 10:23pm UTC
hello,
im trying to make a simple guessing game but for some reason it isnt working..
(the code is working fine but it cant guess the number..)
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#include <iostream>
#include <Windows.h>
using namespace std;
signed int maximum,minimum = 0, input, guess;
void clear() {
system("cls" );
}
bool menu() {
cout << "----------------------" << endl;
cout << " Menu" << endl;
cout << "----------------------" << endl;
cout << "0> Start" << endl;
cout << "1> Quit" << endl;
cin >> input;
clear();
if (input == 0)
return true ; // continue loop
else
return false ; // break out of menu
}
bool story() {
cout << "----------------------" << endl;
cout << " Story" << endl;
cout << "----------------------" << endl;
cout << "blablalbalblalbalblalbalblalbala..." << endl;
cout << "please pick a number between 0 and .." << endl;
cin >> input;
maximum = input;
clear();
cout << "----------------------" << endl;
cout << " Story" << endl;
cout << "----------------------" << endl;
cout << "blablalbalblalbalblalbalblalbala..." << endl;
cout << "please pick a number between 0 and " << maximum << endl;
cout << "blablalbalblalbalblalbalblalbala.." << endl;
cout << "are you ready?.." << endl;
cout << endl << "0> yes" << endl;
cout << "1> no" << endl;
cin >> input;
clear();
if (input == 0)
return false ; // break out of story
else
return true ; // continue this loop
}
bool game() {
clear();
guess = minimum + maximum / 2;
cout << "Computer> is the number : " << guess << "?" << endl;
cout << "0> lower" << endl;
cout << "1> higher" << endl;
cout << "2> yes" << endl;
cin >> input;
if (input == 0)
maximum = guess;
if (input == 1)
minimum = guess;
if (input == 2) {
cout << "Computer> YAYYYYY! :p" << endl;
cout << "(a new game will start in 5 sec..)" << endl;
Sleep(5000);
return false ; // break out of game
}
return true ; // continue this loop
}
int main(){
while (menu()) {
while (story());
while (game());
clear();
}
return 0;
}
any idea why? :\
Last edited on Jun 20, 2015 at 10:24pm UTC
Jun 21, 2015 at 1:20am UTC
I would not call this simple, overly complex would be a better description. Without getting into any of that, it does guess the right number on the second try every time, if you enter an even number. The reason for that is order of precedence.
guess = minimum + maximum / 2;
Even if you fix that, however; it will never guess an odd number because of integer division.