Как передать данные через несколько функций и правильно вызвать их в main?

0

Я пытаюсь вызвать userWeight в userWeight double convert(). Как мне это сделать? Я сталкиваюсь с проблемами, с которыми он не сотрудничает в основном.

#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 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
    "Параметры функции"? Смотрите здесь: Справочные параметры в Си . Возврат функции - это еще один вариант. Глобальные переменные являются еще одним (но, как правило, плохим) выбором.
  • 0
    У вас также есть опечатка: double wieght() .
Показать ещё 3 комментария
Теги:
scope
type-conversion
function
main

1 ответ

1
Лучший ответ

Ты делаешь это неправильно. Вы должны прочитать о сигнатуре функции и прохождении аргументов.

Вы определили weight как функцию, которая не принимает никаких аргументов

double weight() { //...}

но вы вызываете его с помощью некоторого параметра userWeight в основной функции

weight(userWeight);

и этот параметр дополнительно не определен. (И нет: вы не можете вызывать функцию с аргументом, являющимся локальным аргументом в стеке функции, вызываемой из той же области - это технически возможно, но это не то, что вы хотите).

Это должно быть примерно так:

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;
}
  • 0
    .. что именно я должен делать? Теперь я смущен.
  • 0
    userWeight от пользователя уже в функции веса. Я пытаюсь взять эту переменную вместе с их вводом и перенести ту же самую вещь в функцию convert.
Показать ещё 21 комментарий

Ещё вопросы

Сообщество Overcoder
Наверх
Меню