IF/Else Selection Structure Problem

\This is my first post so bare with me guys.

We are studying if/else selection structures in class and I wrote this program.

#include <iostream>
#include <algorithm>
#include <iomanip>


using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;


int main ()
{
//Declare Variables
int code = 0;
double sales = 0;

//Enter Variables
cout << "Enter Code: ";
cin >> code;
cout << "Enter sales: ";
cin >> sales;

if (code == 1 || 2)
{
sales *= .12;
cout << "Result is ";
cout << sales << setprecision(2) << endl;
}
else if (code == 3)
{
sales *= .15;
cout << "Result is ";
cout << sales << setprecision(2) << endl;
}
else if(code == 4)
{
sales *= .20;
cout << "Result is ";
cout << sales << setprecision(2) << endl;
}
else (code > 4 || code < 1);
{
cout << endl;
cout << "error";
cout << endl;
}
//end ifs


return 0;
} //end main


I cannot figure out why the program always displays the error message. I am trying to write it so it displays the error message if the int variable "code" is not 1,2,3 or 4.

Can someone help me please.

PS this is not for a test or anything. It is just a flowchart that was given to me for practice to turn into code. Thanks guys.
Last edited on
code == 1 || code == 2 is the condition you're looking for. code == 1 || 2 always evaluates to true, because (x || true) == true (remember that true is defined as anything other than zero).
Last edited on
else does not have a condition, i.e.:

1
2
3
4
5
6
7
if(i == 1) {
//stuff
} else { /*i != 1*/
//do other stuff
}

Also, you can't do [code]if(code == 1 || 2) 
. You have to do it like this: if(code == 1 || code == 2)

Also, don't put a ';' after the else
Last edited on
I see...Thx guys...
Topic archived. No new replies allowed.