if statement

I'm not exactly sure if I correctly identified my variables and I think that may be the only reason. It could also be the if statements that I tried using.
The output at the moment is

Type your first name (with no space) :
Jeremie
Type metric for metric system, type standard for standard system:
metric
Jeremie, you are considered to be obese.

Here's the code.
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
#include <iostream>
#include <string>
using namespace std;

int main() {
    double weight;
    double height;
    double BMI;
    string system;
    string name;
    string metric;
    string standard;
    cout << "Type your first name (with no space) :\n";
    cin >> name;
    cout << "Type metric for metric system, type standard for standard system:\n";
    cin >> system;
    if (system == metric) {
        cout << "Type your weight in kilograms and your height in meters:\n";
        cin >> weight;
        cin >> height;
        BMI = weight/(height*height);
        cout << name << ", your BMI is " << BMI << "\n";
    }
    else if (system == standard){
        cout << "Type your weight in pounds and your height in inches:\n";
        cin >> weight;
        cin >> height;
        BMI = (weight/(height*height))*703;
        cout << name << ", your BMI is " << BMI << "\n";
    }
    //else 
        //cout << "Please choose metric or standard.\n";
   if (BMI < 18.5)
       cout << name << ", you are considered to be underweight.\n";
   if ((BMI < 25.0) && (BMI >= 18.5))
       cout << name << ", you are considered to be normal.\n";
   if ((BMI >= 25.0) && (BMI < 30.0))
       cout << name << ", you are considered to be overweight.\n";
   if (BMI >= 30.0)
       cout << name << ", you are considered to be obese.\n";
    return 0;
}
1
2
    double BMI; //uninitialized, contains garbage
    string system; //default constructed, it's an empty string 
The name is not the content.
could you elaborate on that? I'm very new to coding c++ and really any coding in general
He means that at line 17 you're comparing the value of string variable system to the value of string variable metric. At that point in the code, you have not assigned a value to the string 'metric', so it is empty ("").

With your example input (the system variable has value "metric", your code is performing the following checks ...

Line 17, compares "metric" with ""
Line 24, compares "metric" with ""

Cheers,
Jim
I mean that `metric' and `standard' are empty strings,
so when you do if (system == metric) is the same as if (system == "")
So both conditions (line 17 and 24) failed and `BMI' were never given a value.

You could if (system=="metric")
Or initialize them string metric="metric";
Last edited on
that fixed it. Thank you very much.
Topic archived. No new replies allowed.