Classes and Objects not working? BMI Calculator Program

Something must be out of order, I think it is the if's or the classes but whenever I run this, the order messes up. If you click English ( choice 2) then the english way to find your BMI should show up, but instead the metric one shows up.

If I switch around Metric and English(making English first) then Metric uses the english way. Not sure how to fix this. Any help? Also, if there any ways to make this more effecient, that would help as well, thanks!
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  #include <iostream>
#include <string>

//BMI CALCULATOR

/*FORMULA
English BMI Formula
BMI = ( Weight in Pounds / ( Height in inches x Height in inches ) ) x 703
Metric BMI Formula
BMI = ( Weight in Kilograms / ( Height in Meters x Height in Meters ) )
*/

using namespace std;

class englishFormula {
    public:
        int englishBMI ()
        {
    unsigned short age;
    double inches, pounds;
    string name;

    cout << "Welcome to Hawk's BMI Calculator! Please follow the directions as shown.";
    cout << "What is your name? ";
    cin >> name;
    cout << "Hello there " << name << "! Now, how old are you in years?\n" << endl;
    cin >> age;
    cout << "\nGreat, two more steps. What's your height in inches?\n";
    cin >> inches; cout << "\nAnd your weight in pounds?" << endl;
    cin >> pounds;

    double english = (pounds/(inches * inches)) *703;

    cout << "Your BMI is " << english;
        }
};

class metricFormula {
public:
    int metricBMI ()
    {
    unsigned short age;
    double kilos, meters;
    string name;

    cout << "Welcome to Hawk's BMI Calculator! Please follow the directions as shown.";
    cout << "What is your name? ";
    cin >> name;
    cout << "Hello there " << name << "! Now, how old are you in years?\n" << endl;
    cin >> age;
    cout << "\nGreat, two more steps. What's your height in meters?\n";
    cin >> meters; cout << "\nAnd your weight in kilograms?" << endl;
    cin >> kilos;

    double metric = (kilos/(meters* meters));

    cout << "Your BMI is " << metric;
    }

};
int main()
{
    short choice;
    cout << "Do you use Metric Units or English Units? " << endl;
    cout << "\nMetric ~ Press 1";
    cout << "\nEnglsih ~ Press 2\n" << endl;
    cin >> choice;

    switch(choice){
    case 1:
        cout << "You chose Metric. \n\n";
        break;

    case 2:
        cout << "You chose English.\n" << endl;
        break;

    default:
        cout << "Please choose one or two!";

    }

    if (1){
        metricFormula metricObject;
        metricObject.metricBMI();
    }
    else if (2)
    {
        englishFormula englishObject;
        englishObject.englishBMI();
    }



    return 0;
}
Last edited on
Instead of

1
2
3
4
5
6
7
8
9
    if (1){
        metricFormula metricObject;
        metricObject.metricBMI();
    }
    else if (2)
    {
        englishFormula englishObject;
        englishObject.englishBMI();
    }


write

1
2
3
4
5
6
7
8
9
    if ( choice == 1){
        metricFormula metricObject;
        metricObject.metricBMI();
    }
    else if ( choice == 2)
    {
        englishFormula englishObject;
        englishObject.englishBMI();
    }
thanks vlad, it worked!

It's weird though, I didn't have to use the choice == on my other project, but on this one I did.

thank you anways!
You could place these statements

1
2
metricFormula metricObject;
metricObject.metricBMI();

and

1
2
englishFormula englishObject;
englishObject.englishBMI();


inside corresponding case blocks of the switch statement above. your if-else.
Should I delete the if-else all together then? Since that should execute the classes if I put it under the switch.
The if-else will be unnecessary in this case.
Thank you, works fine!
Topic archived. No new replies allowed.