If, If else coding problem

Hi I have to write a program that takes 2 inputs to indicate the amount of gas used and then calculate how much that amount of gas would cost according to the chart at the top of my program. The first input would be a 5 digit number for the amount of gas used previous month and the second input would also be a 5 digit number for the amount used for the current month. I feel that my math is wrong somewhere but I cannot figure out where. For example if i put in 0 for the previous month and 52 for the current I'm supposed to get and answer of $29.99

Here is my code,

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

/* RULES: 
First 10 cubic meters $15.21 minimum cost 
Next 30 cubic meters 35.85 cents per cubic meter 
Next 85 cubic meters 33.53 cents per cubic meter 
Next 165 cubic meters 30.26 cents per cubic meter
Above 290 cubic meters 27.95 cents per cubic meter
*/ 
int main()  
{ 
	int first_month;
	int second_month;
    int number; 
    double total = 15.21; 
    double price;
	double total_cost;
    cout<<"Enter your meter number from the previous month: "; 
    cin>>first_month; 
	cout<<"Enter your meter munber from the current month: ";
	cin>>second_month;

	number=second_month-first_month;

    if( number > 290 ) { 
        price = 0.2795;  
		}
	else if( number <= 290  )  
        price = 0.3026; 
    else if( number <= 125 ) 
        price = 0.3353; 
    else if( number <= 85 ) 
        price = 0.3026; 
	else if (number <= 40)
		price = 0.2795;

         
    total_cost = price * number; 

    cout<<("The total price is: $%.", total_cost  ); 
	cout<<endl;
	system("pause");
    return 0; 
}  
For your else if statement starting on line 30 you are saying if the var number is less than or equal to 290 then the price is 30.26 no matter what. You need to use the "and" opperator &&. So it would be like this:

1
2
3
4
else if (number <= 290 && > 125)
   price = 0.3026;
else if (number <= 125 && > 85)
   price = 0.3353;


and so on.. Try this and see if it works
So it should look like this

1
2
3
4
5
6
7
8
9
10
11
 if( number > 290 ) { 
        price = 0.2795;  
		}
	else if( number <= 290 && > 125 )  
        price = 0.3026; 
    else if( number <= 125 && > 85 ) 
        price = 0.3353; 
    else if( number <= 85 && > 40 ) 
        price = 0.3026; 
	else if (number <= 40 && > 10 )
		price = 0.2795;
Yes. That way you are telling the program:

if it is less than or equal to 290 and also greater than 125, then the price is .3026 and so on. Before you just said if it is less than or equal to 290 the price will be .3026 so if you inputted 123 it would still be less than 290, outputting the .3026.
get rid of redundancies.

1
2
3
4
if( number > 290 )
   ...
else if( number <= 290 )
  ...


The "else" here already guarantees that the number will be <= 290 because the previous if condition has failed. You do not have to (and should not) do that comparison again because it is extra work and introduces duplicate code.

Instead, let 'else' do it's job and assume that if you are in an else block, that all of the previous if's have failed:

1
2
3
4
5
6
7
if( number > 290 )
   ...
else if( number > 125 )
   ...
else if( number > 85 )
  ...
 // etc 
Last edited on
It didn't work I am still getting the same output as before all the new >'s I've added say they expected an expression
Your &&'s are ill formed. But you don't need those &&'s (and you shouldn't have them).

See my previous post.
So what you are saying is it should look like my original post minus the = signs?
no, your original post was comparing incorrectly. It should look more like my post.

To clarify, this is what's happening:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if( number > 290 )  // is the number greater than 290?
{
  // yes
}
else
{
  // no, the number is not greater than 290
  if( number > 125 )   // okay... is it greater than 125?
  {
     // yes
  }
  else
  {
    // no, it's not greater than 290, nor is it greater than 125
  }
}


Although instead of nesting them in {braces} like that, C++ let's you shortcut that by just putting else if

So this:
1
2
3
4
5
6
7
8
9
if( foo )
  ...
else
{
  if( bar )
    ...
  else
    ...
}


is the same as this:
1
2
3
4
5
6
if( foo )
  ...
else if( bar )
  ...
else
  ...
Last edited on
I fell that I've gotten closer. But, if i were to have my input at 0 then 52 the price would be $29.99; I'm getting 15.7352. I'm almost 100% sure that I'm still doing something wrong.

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
 if( number > 290 ) //is the number greater than 290
		{ 
        price = 0.2795;  //yes
		}
	else
	{   //no, the number is less than 290
		if( number < 290)  
		{
        price = 0.3026; //yes
		}
	
    else
	{ 
		if( number < 125) 
		{
        price = 0.3353; 
		}
    else
	{
		if( number < 85) 
		{
        price = 0.3026; 
		}
	else 
	{
		if (number < 40)
		{
		price = 0.2795;
				}
			}
		}
	}
}

Last edited on
As an aside, @ADTR2012 the following is not correct:

 
if (number <= 290 && > 125)


It should be
 
if (number <= 290 && number > 125)


Jim
Your unclear indentation makes it difficult to see what's going on.

Anyway... take a look at this. This is the same code, I just changed your comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 if( number > 290 ) //is the number greater than 290
{ 
    price = 0.2795;  //yes, greater than 290 means we get the 0.2795 rate
        // this is correct
}
else
{
    //no, the number is less than (or equal to) 290
    if( number < 290 )    // You know the number is less than 290, so why are you checking
                // to see if it's less than 290 again?  You already know it is!
                //  the only time this will not be true is if number == 290
    {
        price = 0.3026; //  this is wrong.
    }




EDIT:

@Jim: You are correct in that is how you would use &&... but I really want to stress that && should not be used here as it creates duplicate code.
Last edited on
My assignment description is:

Write a program that will process customer's bills. It will take 2 meter readings (both five-digit numbers) indicating the amount of gas used in cubic metres. The first number is for the end of the previous month and the second is the end of the current month. The program should calculate the customer's bills based on the amount of gas used and the table below.

For example, if a customer has 2 meter readings of 56874 and 56926, the gas consumption is 52m cubed. the cost for 52m of gas according to the chart is $29.99.

RULES:
First 10 cubic meters $15.21 minimum cost
Next 30 cubic meters 35.85 cents per cubic meter
Next 85 cubic meters 33.53 cents per cubic meter
Next 165 cubic meters 30.26 cents per cubic meter
Above 290 cubic meters 27.95 cents per cubic meter

Also my first if statement is saying if the NUMBER is Greater than 290
the second one says NUMBER is Less than 290
Last edited on
Yes. I understand the assignment. Do you understand what I'm getting at? I can try to clarify more if it's still unclear, but you'll have to give me direction as to what's confusing you.
THe part i guess I don't understand is how to get the if statements to go between, lets say 40 and 125 but then show between 125 and 290
See my original reply:

1
2
3
4
5
6
7
if( number > 290 )
   ...
else if( number > 125 )
   ...
else if( number > 85 )
  ...
 // etc  


With comments to clarify:

1
2
3
4
5
6
7
8
9
10
11
12
13
if( number > 290 )
{
   // here, we know the number is greater than 290
   //  effective range:  [291,infinity)
}
else if( number > 125 )
{
   // the 'else' ensures that the previous 'if' had failed.  Therefore we know
   //   the number is <= 290
   // we also know the number is > 125 because of the above if.
   //  Therefore, here, the effective range is between [126,290]
}
// etc 
Last edited on
so is it necessary to have { brackets after every if and else or just the first if
It's necessary to have braces if you have multiple things you want the if to do. If you have braces, the if will extend to everything in the braces:

1
2
3
4
5
6
if( something )
{
  AllOfThis();
  WillBePart();
  IfTheIf();
}


If you do not have braces, the if extends only to the next semicolon:

1
2
3
if( something )
  PartOfTheIf(); // <- notice, semicolon
NotPartOfTheIf();  // <- not part of the if, after the semicolon 



Some people say you should always put braces, even when unnecessary. I personally do not agree with that at all.
Last edited on
Topic archived. No new replies allowed.