0

I'm trying to call userWeight into the double convert() function. How do I do this? I'm running into issues with it no cooperating in main.

#include <iostream>
#include <string>

using namespace std;

// health calc

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;

}

int height(string name1) //(string name1) is what we are passing into this function
{
    //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;
    }


}

double wieght()
{
    cout << " How much do you weigh? (In pounds) " << endl;
    double userWeight;
    cin >> userWeight;

    cout << " Ok so your weight in the Imperial System (lbs.), is " << userWeight << endl;
    cout << " Would you like to know what your weight is in the Metric System? (kilograms) " << endl;
cout << " please answer as 'yes' or 'no;" << endl;
string response;
cin >> response;

    if (response == "yes")
    {
        cout << " Alright! Let us start converting your weight! " << endl;
    }
    else if (response == "no")
    {
        cout << " Too bad! We are going to do it anyway! " << endl;
    }
    else
    {
        cout << " That was not a proper response! Way to follow directions!, as consequence, we will do it!" << endl;
    }

    return userWeight;


}

double convert(double userWeight)
{
    cout << " Well since 1 kilogram is equal to 2.2046226218 pounds, we need to divide your weight by that repeating number." << endl;
    cout << " Since that number is very long and ugly, we will use 2.2046 for the sake of clarity." << endl;
    double kiloWeight = (userWeight / 2.2046);
    cout << "Your weight in pounds is " << userWeight << "lbs, divided by 2.2046 gives us" << kiloWeight << "kgs! " << endl;


}


int main()
{

    string name1 = name();
    height(name1);
    weight(userWeight);
    convert();
    return 0;
}
  • 1
    Uh, "function parameters"? Look here: [Reference Parameters in C](http://stackoverflow.com/questions/2564873/reference-parameters-in-c-very-basic-example-please). [Function returns](http://computer.howstuffworks.com/c12.htm) are another option. [Global variables](http://farside.ph.utexas.edu/teaching/329/lectures/node19.html) are another (but generally poor) choice. – FoggyDay Jun 08 '14 at 17:17
  • You also have a typo: `double wieght()`. – Tudor Berariu Jun 08 '14 at 17:20
  • You should either a) switch the tutorial you're doing, b) read further in your tutorial or c) buy a book about c++. – Appleshell Jun 08 '14 at 17:39
  • 1
    tough crowd here on stack – user3719967 Jun 08 '14 at 17:41
  • 1
    Sorry, it's not meant in a negative way, but this questions should be covered on the first pages of every book or tutorial. You'll learn a lot faster reading a comprehensive guide than asking a lot of basic questions. – Appleshell Jun 08 '14 at 17:51

1 Answers1

1

You are doing it wrong. You should read about function signature and passing arguments.

You have defined weight as a function that doesn't take any arguments

double weight() { //...}

but you are calling it with some parameter userWeight in main function

weight(userWeight);

and this parameter in addition is not defined. ( And no: you cannot call a function with argument being local argument on the stack of function being called from same scope - it is technically possible but this is not what you want).

This should be something like:

int main() {

    double userWeight = weight();
    double result = convert( userWeight);
    // we can see here that local variable named userWeight was assigned value 
    // from a call to weight() and this result is now being passed to convert
    // now you can use a result from calling convert
    //...
    return 0;
}
4pie0
  • 29,204
  • 9
  • 82
  • 118
  • ..what exactly should I do? I'm confused now. – user3719967 Jun 08 '14 at 17:25
  • userWeight is from the user already in the weight function. I'm trying to take this variable along with their input and carry the exact same thing into the convert function. – user3719967 Jun 08 '14 at 17:30
  • All the work is being done in the functions and I just want main to subsequently run each function – user3719967 Jun 08 '14 at 17:31
  • the variable put on stack in weight() with double userWeight; cannot be accessed in another function – 4pie0 Jun 08 '14 at 17:33
  • each call to a function creates a new stack and all variables that have been put on this are destroyed as function returns – 4pie0 Jun 08 '14 at 17:39
  • So I can not keep carrying the same value from 'userWeight' throughout the program? I only want it in the next function. I was able to carry a string from one function to another, why can't I do this?! – user3719967 Jun 08 '14 at 17:40
  • You can, if you return value from function to local value to main, then pass this to another function: look at my example – 4pie0 Jun 08 '14 at 17:42
  • @Appleshell double userWeight is not passed to weight but created in it – 4pie0 Jun 08 '14 at 17:43
  • @bits_international Oh right, I was distracted by the code in the question. Misread the comment, sorry. – Appleshell Jun 08 '14 at 17:45
  • what exactly is result here? – user3719967 Jun 08 '14 at 17:47
  • what then has to be in the parameters of the convert function? – user3719967 Jun 08 '14 at 17:50
  • @user3719967 you can see it in my answer – 4pie0 Jun 08 '14 at 17:50
  • not sure what you mean, but what goes in the parameter os the convert function now? – user3719967 Jun 08 '14 at 17:53
  • double userWeight = weight(); double result = convert( userWeight); we can see here that local variable named userWeight was assigned value from calling of weight() and this result is now being passed to convert – 4pie0 Jun 08 '14 at 17:55
  • alright but userWeight does not work in the convert function because it is not declared in that scope (error I got) `double convert()` has no arguments in the parameters, is that why? – user3719967 Jun 08 '14 at 18:04
  • look, convert is declared as "double convert( double something)' and I call it in my answer with "convert( userWeight);" – 4pie0 Jun 08 '14 at 18:06
  • I'm talking about the function OUTSIDE of main, tak a look at my code – user3719967 Jun 08 '14 at 18:08
  • even if `userWeight` is in those parameters, I stil legt the error that it is not declared in that scope – user3719967 Jun 08 '14 at 18:09
  • yes, your code is not correct, you should change it taking into account my hints. Delete a body of functions, just play with passing parameters first – 4pie0 Jun 08 '14 at 18:09
  • i'm confused. is there no way you can just tell me exactly what's wrong? there is no one I'm going to get this now. I kept tweaking this to what you were saying and nothing – user3719967 Jun 08 '14 at 18:16
  • I was not giving the datatype of userWeight within the parameters of `double convert` ... that was my issue.. the code ran – user3719967 Jun 08 '14 at 18:24