Hello joe100,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
Going over your program:
#include <bits/stdc++.h>
. This is not s standard C++ header file and you should be learning not to use it. I do believe this will include more header files than you need and along with:
using namespace std;
you are more likely to get into trouble.
This is what I came up with for a start:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
//This program guesses the user's number between 1 and 1 million in 20 guesses or less.
#include <iostream>
#include <cctype> // <--- For "std::tolower() and std::toupper()" + others.
using namespace std; // <--- Best not to use.
void greeting();
//void firstGuess(int guess);
void guesses();
int main()
{
greeting();
//firstGuess(guess);
guesses();
return 0; // <--- Not required, but makes a good break point.
}
|
This is all you will need and the 2nd "include" is optional.
Unless "firstGuess" is a required function you can do without it.
For "greatings" I did this:
1 2 3 4 5 6 7 8
|
void greeting()
{
cout <<
"\n"
" Hello. Pick a number between 1 and 1,000,000 \n"
" and I can guess your number in 20 guesses or less.\n\n"
" Tell me if my guess is high, low, or correct. \n";
}
|
By putting each line in double quotes it looks more like what you will see on the screen and the IDE and compiler considers each string as just 1 big string. This is just easier to work with.
I do not know what you will be running this progrram on and how wide the screen is, but I find it better to keep the lines short and easy to read. Also when you reach the right side of the screen the line will wrap, but it could wrap in the middle of a word.
Without using the "firstGuesss" function you can do this in the "guessed" function:
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
|
void guesses()
{
char /*H, L, C,*/ response{};
int count{ 1 };
int guess{ 500000 };
int min = 1, max = 1000000;
cout << "\n Is your number " << guess << "? |H|L|C|: ";
while (response != 'C' && response != 'c' && count <= 20)
{
cin >> response;
if (std::isalpha(response))
{
response = std::toupper(response); // <--- Requires header file "<cctype>".
}
else
{
std::cerr << "\n Please enter a letter (H, L, or C)\n";
cout << "\n Is your number " << guess << "? |H|L|C|: ";
continue;
}
if (response == 'H' || response == 'h')
{
max = guess;
guess = (max + min) / 2;
cout << "\n Is your number " << guess << "? |H|L|C|: ";
count++;
}
|
Note the change in the while loop to check for both cases.
I did have to move "count" inside the 2 if statements for "H" and "L" so that the count would be correct. Where you have it if a "C" is entered you add 1 to "count" after bypassing the 1st 2 if statements.
I had to change this line:
if ( response != 'C' && count == 20 )
to:
if (response != 'C' && response != 'c' && count >= 20)
. If you change the case after you give "response" a value then you can delete the check for the lower case "c".
The last thought I had is at the end of the function you should check for "response" not being == "H","L" or "C". You could probably just use an if or if/else for this.
A short sample run:
Hello. Pick a number between 1 and 1,000,000
and I can guess your number in 20 guesses or less.
Tell me if my guess is high, low, or correct.
Is your number 500000? |H|L|C|: h
Is your number 250000? |H|L|C|: l
Is your number 375000? |H|L|C|: c
I guessed your number in 3 guesses. Thanks for playing!
|
Andy