|
|
|
|
The coding of this program does require an abstracted main(), which means that subprograms will be used to implement the various functions involved. For example, a separate function will display the greeting and a different function will ask each new guess, and another function will report the results of the game. |
#include <math.h>
. This is a C header file and you should be using the C++ version "<cmath>". That is the correction, but the header is not needed for what you are doing. The operators (+, -, *, /, %) are built in to the IDE and compiler. Nothing special is needed to use them. Have a look at http://www.cplusplus.com/reference/cmath/ This will show you all the functions available in the header file.if(choice=='h' || choice=='H')
could be written as: if(std::toupper(choice) =='H')
. Then it would not matter what case was entered.cout<<"Your number is: " <<high<<endl;
would be all that you need.
|
|
Hi, my name is Brent.Think of a number between 1 - 1,000,000 and I'll try to determine it in 20 guesses or less. You just tell me if my guess is too Low, too High, or Correct [l, h, c]. 1. Is your number 500001 [l, h, c]: h 2. Is your number 250001 [l, h, c]: h 3. Is your number 125001 [l, h, c]: h 4. Is your number 62501 [l, h, c]: h 5. Is your number 31251 [l, h, c]: h 6. Is your number 15626 [l, h, c]: h 7. Is your number 7814 [l, h, c]: h 8. Is your number 3908 [l, h, c]: h 9. Is your number 1955 [l, h, c]: h 10. Is your number 978 [l, h, c]: h 11. Is your number 490 [l, h, c]: l 12. Is your number 734 [l, h, c]: h 13. Is your number 612 [l, h, c]: l 14. Is your number 673 [l, h, c]: h 15. Is your number 643 [l, h, c]: l 16. Is your number 658 [l, h, c]: h 17. Is your number 651 [l, h, c]: l 18. Is your number 655 [l, h, c]: c I win! I guessed your number in only 18 guesses |
|
|
|
|
|
|
g++ -std=c++17 "20 Guesses.cpp" -o "20 Guesses.exe" |
|
|
|
|
lb(1), ub (maxnum)
mean?guesses += (input == 'l' || input == 'h')
mean?
|
|
|
|
char choice;
const int max_value =100;
int guess=0, high=max_value, low=min_value;
int input {'l'}, lb {1}, ub {maxnum};
is that the initializer syntax has {} and not =.
|
|
input == 'l'
returns true or falseinput == 'h'
returns true or false||
is logical OR. x||y
is false only if x and y are both false.(input == 'l' || input == 'h')
is true or false.false
converts to 0. Therefore, guesses += false
is same as guesses += 0
true
converts to 1. Therefore, guesses += true
is same as guesses += 1
|
|