Improving Program

Hi again I've been going through my lessons and was wondering if was possible to execute and condition statement inside of a function such as

float convert ( float moles, atoms, grams )
{
if (

and so on and so on. If it would please let me know.
If that wouldn't work would I be able to make multiple functions linking each specific equation to a function and then having a switch case activate the equation for me or even a conditional statement. Im new to this if none of this will work please tell every thing thats wrong with be harsh. Thanks for any answers. whOOper.
Do you mean using a conditional statement in a function?

Yes, that is perfectly alright.
closed account (z05DSL3A)
Here is a sample of a convert function (two implementations, one with if..else and one with a switch)

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

enum Conversion{cm2inch, inch2cm};
/*
double convert(double in, Conversion conv)
{
    double out =0;
    switch (conv)
    {
        case cm2inch: 
            {
                out = (in / 2.54); 
            }
            break;

        case inch2cm: 
            {
                out = (in * 2.54); 
            }
            break;

        default: 
            break;
    }
    return out;
}
*/
double convert(double in, Conversion conv)
{
    double out =0;

    if (conv == cm2inch)
    {
        out = (in / 2.54);
    }
    else if (conv == inch2cm)
    {
        out = (in * 2.54);
    }
    else
    {
    }

    return out;
}

int main()
{
    std::cout << convert(3, inch2cm) << std::endl;
    std::cout << convert(3, cm2inch) << std::endl;
}
Topic archived. No new replies allowed.