简体   繁体   中英

Simple Function C++

using namespace std;
int main()
{

return 0;
}
double C2F()
{
    f = value * 9 / 5 + 32;
    return f;
}

double K2F()
{
    f = (value - 273.15) * 1.8 + 32.0;
    return f;
}

double N2F()
{
    f = value * 60 / 11 + 32;
    return f;
}

Im having trouble calling these functions to calculate the temperature conversion instead of calculation it from within the cases. The program will not even compile after adding these functions. "Error: Expected a ";""

You cannot declare or define functions inside another function. Move your definitions outside of int main(){ ... } .

First of all you cant declare another function in the main() function.

secondly all of your function have a return type, but surprisingly you are calling them as they are void. Make the function void instead of a return type. As for example....

void C2F()
{
    f = value * 9 / 5 + 32;
}

and then

case 'C':
        C2F();
        cout << value << "C is " << f << " in Farenheit" << endl;
        break;

OR. You can receive the return value in a double type variable and and print the value.

case 'C':
    cout << value << "C is " << C2F() << " in Farenheit" << endl;
    break;

This is what you want.

  1. Declare functions outside the main method.
  2. Declare functions before the main method or use forward declarations
  3. Pass the 'value' to each function as a argument.
  4. Remove unnecessary variable declaration.
  5. Use some validation for user inputs.
  6. Use meaningful variable names.

include

using namespace std;


double C2F(double f)
{       
    return f * 9 / 5 + 32;    
}

double K2F(double f)
{   
    return ((f - 273.15) * 1.8 + 32.0);   
}

double N2F(double f)
{           
    return (f * 60 / 11 + 32);

}

int main()
{
    char function;
    double value;
    cout << "This temperature Conversion program converts other temperatures to farenheit" << endl;
    cout << "The temperature types are" << endl;
    cout << "" << endl;
    cout << "C - Celcius" << endl;
    cout << "K - Kelvin" << endl;
    cout << "N - Newton" << endl;
    cout << "X - eXit" << endl;
    cout << "" << endl;
    cout << "To use the converter you must input a value and one of the temperature types." <<         endl;
    cout << "For example 32 C converts 32 degrees from Celsius to Fahrenheit" << endl;
    cin >> value >> function;

    function = toupper(function);

    while (function != 'X')
    {
        switch (function)
        {
        case 'C':       
            cout << value << "C is " << C2F(value) << " in Farenheit" << endl;
            break;
        case 'K':       
            cout << value << "K is " << K2F(value) << " in Farenheit" << endl;
            break;
        case 'N':
            cout << value << "N is " << N2F(value) << " in Farenheit" << endl;
            break;
        default:
            cout << "Correct choices are C, K, N, X" << endl;
        }
        cout << "Please enter a value and it's type to be converted" << endl;
        cin >> value >> function;
        function = toupper(function);
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM