Problem with easy solution

Hello i'm new to programming and wondering what is wrong with this code I wrote.
I am on mac osx 10.7 using code blocks.



#include <iostream>

using namespace std;

int main()
{
int myNumber;

cout << "Please enter a number between 1 and 10..." << endl;
cin >> myNumber;


if (myNumber < 5) {
cout << "Your number is less than 5!\n";
}
else (myNumber > 5); {
cout << "Your number is greater than 5!\n";
}

if (myNumber == 5) {
cout << "Your number is equal to 5!\n";
}
else {
}

return 0;
}

_________

When I enter a number less than 5 the output shows both greater than and less than answers. When i enter a number equal to 5 the output shows both equal to and greater than answers. When I enter a number greater than 5 the program runs smooth.

Thank you for your time.
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
// First off, else statements are generally coded like this:
else // An else statement without braces { } will execute one statement.
    /* do something here*/;

// or
else // An else statement with braces { } will execute whatever's in between the two braces
{
    /* do several things here*/;
}

else (myNumber > 5);
// This is incorrect, what you want is this:
else if (myNumber > 5) // note how I omitted the semi-colon
    cout << "Your number is greater than 5!\n";

else
{

}
// Since this else does nothing, feel free to remove it.
// It is not required for your program to function properly.

// Also, for every if statement, you can have infinite else if statements, so feel free to do this:
if (/* test expression */)
{

}
else if (/* test expression */)
{

}
else if  (/* test expression */)
{

}
// so on and so forth. 


Let me know if you have any questions or require me to go into greater detail.
Last edited on
One thing to point out, technically there is no such thing as else if structures in C++. The workaround is what you see in Phil's post. How if/else if/else chains really work:
1
2
3
4
5
6
7
8
9
10
11
12
if (condition1)
   // condition1 was true;
else
   // condition1 was false
   if (condition2)
      // condition1 was false, condition2 was true;
   else
      // condition1 and condition2 were false
      if (condition3)
         // condition1 and condition2 were false, condition3 was true;
      else
         // All of the conditions were false; 


The same code written with brackets for clarification:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (condition1) {
   // condition1 was true;
}
else {
   // condition1 was false
   if (condition2) {
      // condition1 was false, condition2 was true;
   }
   else {
      // condition1 and condition2 were false
      if (condition3) {
         // condition1 and condition2 were false, condition3 was true;
      }
      else {
         // All of the conditions were false;
      }
   }
}

This entire time you've been using unbracketed else statements and not knowing it. And as Phil also pointed out, if an else isn't used, you can feel free to omit it. The only reason a programmer would leave an empty else structure is in case they plan on adding an else statement later on.
Last edited on
This entire time you've been using unbracketed else statements and not knowing it.


Hah, the things my C++ books don't teach me...good to know, thanks.
Thank you, it works fine now. I knew generally how if statements worked because I did a little bit of vb.net about a year ago, but this clears things up.
Topic archived. No new replies allowed.