Using values in multiple functions

I am trying to put a value from one function into another function but I have not found anything explaining how to do that for my situation. I am trying to get guess in my guesses function to come from the guess in my firstGuess function without defining it in my guesses function. I currently have "int guess = 500,000 to make the program run.

//This program guesses the user's number between 1 and 1 million in 20 guesses or less.
#include <bits/stdc++.h>
using namespace std;

void greeting();
void firstGuess();
bool guesses();

int main () {
greeting();
firstGuess();
guesses();
return 0;
}

void greeting(void) {
cout << "Hello. Pick a number between 1 and 1,000,000 \n"
<< "and I can guess your number in 20 guesses or less. Tell me if my guess is high, low, or correct. \n";
}
void firstGuess() {
int guess = 500000;
cout << "Is your number " << guess << "? |H|L|C| \n";
}
bool guesses(guess) {
char H, L, C, response;
int count = 0;
int min = 1, max = 1000000;
while( ( response != 'C' ) && ( count <= 20 ) ) {
cin >> response;
if ( response == 'H' ) {
max = guess;
guess = (max + min) / 2;
cout << "Is your number " << guess << "? |H|L|C| \n";
}
if ( response == 'L' ) {
min = guess;
guess = ( (max - min) / 2 ) + min;
cout << "Is your number " << guess << "? |H|L|C| \n";
}
count++;
if ( response != 'C' && count == 20 ){
cout << "You Win... \n";
}
if ( response == 'C' ) {
cout << "I guessed your number in " << count << " guesses. Thanks for playing! \n";
}
}
}
You want to do something like
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
void greeting();
int firstGuess();
bool guesses(int guess);

int main () {
    greeting();
    int first = firstGuess();
    guesses(first);
    return 0;
}

void greeting(void) {
    cout << "Hello. Pick a number between 1 and 1,000,000 \n"
    << "and I can guess your number in 20 guesses or less. Tell me if my guess is high, low, or correct. \n";
}

int firstGuess() {
    int guess = 500000;
    cout << "Is your number " << guess << "? |H|L|C| \n";
    return guess;
}

bool guesses(int guess) {
  // your code
}


There's a bunch of issues here.

First and foremost please put your code in code tags.

Here: void greeting(void)

C++ does not require you to pass void in to function parameters, thats a C thing.

Here: bool guesses(guess) you need to give guess a type, so it would be:

bool guesses(int guess)

Also you have this function as bool, but do not return one, you need to return a value.

Here: char H, L, C, response;

It looks like you're trying to create an array of chars, but response cannot be a char because a char is one character. did you mean to do: char response[3] = { 'H', 'L', 'C' };?

in you're if statements you would do:

response[0] == 'H'

But you could just use a string here.

EDIT: Salem c's answer is more of what you want to do.
Last edited on
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
salem c Thank you! That is what I wanted to do just wasn't sure how to write everything to make the guess carry over to the next function. I will also read up on how to properly post code so it is easier to read for those trying to help. Thank you all!
Topic archived. No new replies allowed.