Extremely Stuck... Condition Statement

Hey guys, this is a simple multiple-alternative decision program. For some reason the program likes to come up with two true statements. I'll post the code below with comments as to what's happening. It's weird because if I put in 2000 as the input, the result comes out 900, but if I input 500 the program displays both 300 and 225. I've been working on this for the past 3 hours or so. I keep getting stuck. I'm thinking that it's the order of the IF statements. Any help would be greatly appreciated. Thanks guys.

---------------------------------------------------------------------------------

#include <iostream>
using namespace std;

int main()
{
const double NORMAL_RATE = 0.60;
const double REDUCED_RATE = 0.45;

cout << "Enter the number of Kilowatt Hours (KWH): ";
double kwh;
cin >> kwh;

double total;


if (kwh <= 1000 and kwh >= 0)
{
total = kwh * NORMAL_RATE;
cout << "You need to pay $" << total << endl;
}

else if (kwh < 0)
{
cout << "Error: You entered a negative KWH.\n";
}

else (kwh > 1000); // Seems like it's ignoring the operator
{
total = kwh * REDUCED_RATE;
cout << "You need to pay $" << total << endl;
}


system("pause");
return 0;
}
That last "else" either needs to be an "else if()" or get rid of the () argument.
Instead of

else (kwh > 1000); // Seems like it's ignoring the operator

should be

else if (kwh > 1000) // Seems like it's ignoring the operator

Do not forget to remove the semicolon at the end of the line.
closed account (Dy7SLyTq)
well yes because else doesnt take a condition. so what the compiler really sees is:
1
2
3
4
5
6
7
else (kwh > 1000); /* it sees the kwh as a command becuase
                                   a) it has a semicolon so it thinks it takes that as its
                                   command and b) because else doesnt take a condition. it defeats the purpose */

{
//rest of code
}
Thanks guys! I seriously do appreciate it. I've been reading my book like crazy, but I keep interpreting things wrong. I mean if it's a conditional statement, it should stop once one scenario becomes true, but I see that's not always the case if I don't write things properly! lol
Topic archived. No new replies allowed.