Simple If Else statement is not working for some reason

So I have a task which i have to

"calculate a monthly electricity bill, a standing charge of €20 is added to
a charge of €0.05 per unit of electricity used. The number of units used is
based on the difference between a previous and present meter reading.
The meter has a maximum reading of 9999, after which it returns to 0000.
Write a program that will accept as input a pair of values representing the
previous and present meter readings respectively. The program should
then calculate and display an electricity bill. Allow for the fact that the
previous meter reading may be greater than the present reading if the
meter has passed 9999 and returned to 0000. Assume that 10000 units of
electricity will never be used in one month."

My if else statement is not working if you could take a look what I am doing wrong because I cannot see anything at all wrong with it.

I also included <iostream> in stdafx.h

using namespace std;

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
 int main()
{
   
	double standingCharge = 20.00;
	int presentMeterReading;
	int previousMeterReading;
	double unitPrice = 0.05;
	int unitsUsed;
	double unitCharge;
	double totalCharge;
	
	cout << "Please enter the previous meter reading on your meter : ";
	cin >> previousMeterReading;
	cout << "\n\n";

	cout << "Please enter the present meter reading on your meter : ";
	cin >> presentMeterReading;
	cout << "\n\n";



	if (previousMeterReading < presentMeterReading)

	{

		unitsUsed = presentMeterReading - previousMeterReading;

	}
		 
	else (previousMeterReading > presentMeterReading);
	{
		unitsUsed = 10000 + presentMeterReading - previousMeterReading;
	}
	
	

	cout << "The total units of electiricty you used this month : " << unitsUsed;
	cout << "\n\n";

	unitCharge = unitsUsed * unitPrice;
	totalCharge = unitCharge + standingCharge;


	cout << "The unit price is 0.05c and there is a 20 euro standing charge.";
	cout << "\n\n";
	cout << "The total cost of your electricty bill comes to " << totalCharge;
	cout << "\n\n";


The problem is the calculations in the If else statement but they look perfectly fine, this is what outputs on the command prompt below.

Please enter the previous meter reading on your meter : 8000

Please enter the present meter reading on your meter : 9000

The total units of electricty you used this month : 11000

or

Please enter the previous meter reading on your meter : 0100

Please enter the present meter reading on your meter : 9000

The total units of electricty you used this month : 18900
Last edited on
1
2
3
4
else (previousMeterReading > presentMeterReading);
	{
		unitsUsed = 10000 + presentMeterReading - previousMeterReading;
	}


Two things wrong here. That's not how an else statement works. Let me give you an example

1
2
3
4
5
6
7
8
if (x ==1)
{
   // do this
}
else 
{
    // do that
}


What you're probably looking for is the if-else statement. Which looks like this :

1
2
3
4
5
6
7
8
if (x ==1)
{
   // do this
}
else if (x == 2) // notice I have no semicolon like you have, remove it.
{
   // do that
}
Last edited on
1
2
3
4
5
6
7
8
9
if  (previousMeterReading < presentMeterReading)
	{
		unitsUsed = presentMeterReading - previousMeterReading;
	} 
	
	if else (previousMeterReading > presentMeterReading)
	{
		unitsUsed = 10000 + presentMeterReading - previousMeterReading;
	}


I did what you said but there's a squigily line coming up below the else in the "if else" ?

says error expected '('
Whoops I totally messed that up. It should obviously be else if, not if else. I'll edit my post.


1
2
3
4
5
6
7
8
if (x ==1)
{
   // do this
}
else if (x == 2) // notice I have no semicolon like you have, remove it.
{
   // do that
}


Edit:
Here is two playlists I always recommend for learning c++

https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83
https://www.youtube.com/playlist?list=PL2DD6A625AD033D36

This site's tutorial is also very nice. Google too.
Last edited on
What happens if previousMeterReading == presentMeterReading?

By the way you really should try to end an if/else if chain with an else to insure you catch all eventualities.
Haha the best of us make mistakes, thanks for the help guys.
Topic archived. No new replies allowed.