0

In my program ( I will include code below ), I have a function to determine the user's name and height. I use the name function first void name() and then the function void height() following it (of course main is last).

What I'm trying to do is to display the user's name throughout the program. In my second function, void height() Is ask the user how tall they are:

cout << " How tall are you?" << endl;

I would like to ask " How tall are you, name1?" , but the string name1 is not declared in the scope. Any ideas of how to make it work / what I'm doing wrong? Thank you. Also if you see any other issues or something I can do to make things easier/alternative ways, please let me know! (I'm new!)

#include <iostream>
#include <string>

using namespace std;


void name()
{
    cout << "Welcome ________ ... uhmmmm, what was your name again?   ";
    string name1;
    cin >> name1;
    cout << " " << endl;
    cout << " Oh that's right! Your name was " << name1 << ", how could I forget that?!" << endl;

}

void height()
{
    //feet and inches to inches
    cout << " How tall are you?" << name1 << endl;
    cout << " " << endl;
    cout << " " << endl;
    cout << " Enter feet:    ";
    int feet;
    cin >> feet;
    cout << " " << endl;
    cout << " Enter inches:    ";
    int inches;
    cin >> inches;
    int inchesheight;

    inchesheight = (feet * 12) + inches;

    cout << " " << endl;
    cout << " Your height is equal to " << inchesheight << " inches total." << endl;


    if (inchesheight < 65 )
    {
        cout << " You are shorter than the average male." << endl;
    }
    else if (inchesheight > 66 && inchesheight < 72)
    {
        cout << " You are of average height." << endl;
    }
    else
    {
        cout << " You are taller than average." << endl;
    }
}




int main()
{
    name();
    height();
    return 0;
}
Rakib
  • 7,435
  • 7
  • 29
  • 45
  • 1
    Any [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) will have this information pretty early on. – chris Jun 08 '14 at 15:50
  • 1
    I'm aware, I don't exactly have access to one because all the good recommended books are a bit expensive. I've been using [this](http://www.learncpp.com/cpp-tutorial/15-a-first-look-at-operators/) website. – user3719967 Jun 08 '14 at 15:54
  • I notice the previous section has information on return values and parameters. – chris Jun 08 '14 at 16:10

1 Answers1

1

Return a string instead of void.

string name()
{
    cout << "Welcome ________ ... uhmmmm, what was your name again?   ";
    string name1;
    cin >> name1;
    cout << " " << endl;
    cout << " Oh that's right! Your name was " << name1 << ", how could I forget that?!" << endl;
    return name1;
}

Same thing with height(), for example, that should return an int. Also to get the name in your height function you could do.

int height(string name1)
{
    // cout stuff about name
    return userHeight;
}

Then you can call it like this:

int main()
{
    string userName = name();  // takes the return from name and assigns to userName
    int userHeight = height(userName);   // passes that string into height()
    return 0;
}

More examples of using functions and returning things:

int add(int a, int b)
{
    int total = a + b;   // the variable total only exists in here
    return total;
}

int add4Numbers(int w, int x, int y, int z)
{
    int firstTwo = add(w, x);  // I am caling the add function
    int secondTwo = add(y,z);  // Calling it again, with different inputs
    int allFour = add(firstTwo, secondTwo);   // Calling it with new inputs
    return allFour;
}   // As soon as I leave this function, firstTwo, secondTwo, and allFour no longer exist
    // but the answer allFour will be returned to whoever calls this function

int main()
{
    int userA = 1;
    int userB = 7;
    int userC = 3;
    int userD = 2;

    int answer = add4Numbers( userA, userB, userC, userD )  // this grabs the value from allFour from inside the add4Numbers function and assigns it to my new variable answer
    return answer;  // now equals 13
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Thanks a lot! The only bit I was learning about functions was [here](http://www.learncpp.com/cpp-tutorial/15-a-first-look-at-operators/). What you just showed here is helping me understand it better! I've mostly understood everything up to this point except I get a little lost when it comes to functions. The second part you wrote about calling it confuses me a little bit. Where exactly would I do that? – user3719967 Jun 08 '14 at 15:53
  • You would call those two lines within `main`. Because right now, you just call the functions, then they execute, then all the variables fall out of scope. To pass something to another function, you need to return it. – Cory Kramer Jun 08 '14 at 15:56
  • Am I calling those two variables in main or am I still calling the functions there as well? I'm not really sure what you mean. Do I need to return something within the functions or main? – user3719967 Jun 08 '14 at 15:59
  • Modify your function `name()` to `return name1` at the end. Then in `main` you can say `string userName = name()` it will take whatever is returned from `name()` and assign it into `userName`. You can then pass that variable around in `main`, and pass it into `height()` if you want to use it in there. – Cory Kramer Jun 08 '14 at 16:01
  • Please see my above edit, hopefully that's more clear. – Cory Kramer Jun 08 '14 at 16:02
  • OH ok I get it now. I guess I just need to learn how to return things and the syntax of recalling that in main. I don't necessarily care about using the height again, so is it necessary that I return it? – user3719967 Jun 08 '14 at 16:07
  • Never mind I tested it out myself and I would just have to reference the height function along with what was in the parameters. Thank you for all your help! Now if it wouldn't be TOO much of a bother can you briefly explain what exactly is going on / happened when that change was made? – user3719967 Jun 08 '14 at 16:09
  • Basically, there is a concept called [scope](https://en.wikibooks.org/wiki/C%2B%2B_Programming/Scope/Examples) that controls when variables exist. If you declare a variable inside a function, it "dies" as soon as the function ends, and nothing else can use that variable. This is an incredibly important concept once you start to learn about Object-Oriented Programming (OOP) which is using classes. Get a firm grasp on using functions first, then I would recommend learning about classes. – Cory Kramer Jun 08 '14 at 16:12
  • Thank you very much! Can you explain what is happening in main with `string name1 = name();` Why is it like that? Is it because the string derives from that function so we are assigning it to that in main? – user3719967 Jun 08 '14 at 16:16
  • You are making a brand new string called `name1`, it doesn't have to be the same name as the one in the function, it could be `string bob`. Then you call `name()` which returns a copy of the string that was `cin>>` in your function. It takes that copy, and assigns it to `name1` or `bob` or whatever. Then you can take that new string and do whatever you'd like with it. – Cory Kramer Jun 08 '14 at 16:17
  • Ok, so where exactly in main am I telling my program to run the name function specifically? It is a little more clear for height because `height(name1);` is just the name of the function so it's just straight calling it. Also I'm assuming that `name1` within `height(name1);` is the exact string already existing from the name function, or from the height function because I already brought it into it? – user3719967 Jun 08 '14 at 16:25
  • Any answer to this, as I want to continue to use more functions and pass data around. – user3719967 Jun 08 '14 at 17:04
  • Please see my most recent edit. See how I can call the function `add()` within `add4Numbers()` using the returned value to define variables in that function? Then I can call `add4Numbers()` in `main()` and not worry about what's going on in the function, only that it will return the correct answer. Does that example make sense? – Cory Kramer Jun 08 '14 at 18:49
  • Got it, you clarified a lot for me! So now say in main I have `string name1 = name();` this is taking the return value from `name()` and assigning it to a defined string I make us called `string name1` ? So if that is assigning my variable and such, which part of that actual code is telling the program to initially run the name function ? – user3719967 Jun 08 '14 at 19:14
  • Your program will by default call the function `main`. It only calls the `name()` function because you called it in `main()`. That is the whole purpose of functions, is that they can call each other whenever you need them. Same thing with your `height()` function, it only executes that code because you call the function in `main`. – Cory Kramer Jun 08 '14 at 19:17
  • ok so by even saying `string name1 = name();` I am taking the returned value of name1 while ALSO calling the `name()` function? I guess I wouldn't be able to assign it without calling it. – user3719967 Jun 08 '14 at 19:39
  • Yes, you are calling the function `name()`, then putting the returned value into the variable `name1`. So it is both a variable declaration and a function call in one step. – Cory Kramer Jun 08 '14 at 20:15
  • OK, thank you! That just about clears it up! – user3719967 Jun 08 '14 at 21:23