Help with IF

The last code I wrote was very similar to this one, with the exception of different formulas. I'm having an issue with the any mass greater than 1000, it displays both cout statements for the weight. I'm unsure what is wrong, with the last code it ran smoothly and I haven't changed anything. I'll post both codes. The first is the one I'm stuck on. Second is for comparison.

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
  #include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    float mass1, mass2, mass3, weight1, weight2, weight3;

    cout << "DATA 1 OBJECT MASS: ";
     cin >> mass1 ;
    weight1 = mass1 * 9.8;
    cout << setprecision(3) << fixed
         << "\nWEIGHT FOR DATA 1: " << weight1
         << " NEWTONS";
    if(weight1 > 1000)
    {
        cout << "\nTOO HEAVY\n";
    }
        else if(weight1 < 10);
        {
            cout << "\nTOO LIGHT\n";
        }


    cout << "\nDATA 2 OBJECT MASS: ";
     cin >> mass2 ;
    weight2 = mass2 * 9.8;
    cout << setprecision(3) << fixed
         << "\nWEIGHT FOR DATA 2: " << weight2
         << " NEWTONS";
    if(weight2 > 1000)
    {
        cout << "\nTOO HEAVY\n";
    }
        else if(weight2 < 10);
        {
            cout << "\nTOO LIGHT\n";
        }


    cout << "\nDATA 3 OBJECT MASS: ";
     cin >> mass3 ;
    weight3 = mass3 * 9.8;
    cout << setprecision(3) << fixed
         << "\nWEIGHT  FOR DATA 3: " << weight3
         << " NEWTONS";
    if(weight3 > 1000)
    {
        cout << "\nTOO HEAVY\n";
    }
        else if(weight3 < 10);
        {
            cout << "\nTOO LIGHT\n";
        }
    return 0;
}


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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
    float height1,height2,height3, weight1,weight2, weight3, bmi1,bmi2,bmi3;
    string in, lbs;

    cout << "Enter DATA 1: \n";
     cin >> height1 >> in >> weight1 >> lbs;

    cout << "\nEnter DATA 2: \n";
     cin >> height2 >> in >> weight2 >> lbs;

    cout << "\nEnter DATA 3: \n";
     cin >> height3 >> in >> weight3 >> lbs;

     bmi1 = weight1 * (703 / pow(height1, 2.0));
     bmi2 = weight2 * (703 / pow(height2, 2.0));
     bmi3 = weight3 * (703 / pow(height3, 2.0));

     if(bmi1 >= 18.5 && bmi1 <= 25)
     {
         cout << "\nOPTIMAL WEIGHT\n";
     }
        if(bmi1 < 18.5)
        {
            cout << "\nUNDERWEIGHT\n";
        }
            if(bmi1 > 25)
            {
                cout << "\nOVERWEIGHT\n";
            }
    cout << setprecision(2) << fixed
         << "DATA 1 BMI = " << bmi1 <<endl;


     if(bmi2 >= 18.5 && bmi2 <= 25)
     {
         cout << "\nOPTIMAL WEIGHT\n";
     }
        if(bmi2 < 18.5)
        {
            cout << "\nUNDERWEIGHT\n";
        }
            if(bmi2 > 25)
            {
                cout << "\nOVERWEIGHT\n";
            }
    cout << setprecision(2) << fixed
         << "DATA 2 BMI = " << bmi2 <<endl;

    if(bmi3 >= 18.5 && bmi3 <= 25)
     {
         cout << "\nOPTIMAL WEIGHT\n";
     }
        if(bmi3 < 18.5)
        {
            cout << "\nUNDERWEIGHT\n";
        }
            if(bmi3 > 25)
            {
                cout << "\nOVERWEIGHT\n";
            }
    cout << setprecision(2) << fixed
         << "DATA 3 BMI = " << bmi3 << endl;

        return 0;
}
Last edited on
> else if(weight1 < 10);
Yes, watch these trailing ; messing you up.

You wrote
1
2
3
4
5
6
7
8
        else if(weight1 < 10)
        {
            // do nothing
        }
        // This is not guarded by your logic, so happens all the time.
        {
            cout << "\nTOO LIGHT\n";
        }

I apologize, I don't understand what you said. You showed code that you said that I wrote, but I have a semicolon after the else if statement. While you do not. So do i need to remove that semicolon?
Yes, remove that ; if you want "TOO LIGHT" to be an actual part of your if / else logic.

The ; acts like { } in the context you have written it.
Why do some IF statements have a semicolon after them? Not in this code specifically, but in general. If i had a code that asked for a range but being unable to use && would I just use two IF's to satisfy the range?
You mean like this?
1
2
3
4
    if(weight1 > 1000)
        cout << "\nTOO HEAVY\n";
    else if(weight1 < 10)
        cout << "\nTOO LIGHT\n";


The syntax is
if ( expression ) statement;
or
if ( expression ) { statement; statement; ... }

It's not a mix-and-match deal, you pick one or the other.

if ( expression ) ; statement;
Means that statement; has NOTHING to do with the if ( expression ), which becomes fully resolved at the first ;


Just try this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

void foo ( ) {
    if ( false )
        cout << "It's true in foo" << endl;
}
void bar ( ) {
    if ( false ); //!! watch the birdie, smile for the camera!
        cout << "It's true in bar" << endl;
}

int main()
{
  foo();
  bar();
  return 0;
}

The formatting might look right, but the compiler doesn't care about formatting at all (this isn't Python).

Last edited on
Topic archived. No new replies allowed.