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
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
elseif( number <= 290 )
price = 0.3026;
elseif( number <= 125 )
price = 0.3353;
elseif( number <= 85 )
price = 0.3026;
elseif (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:
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.
if( number > 290 )
...
elseif( 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 )
...
elseif( number > 125 )
...
elseif( number > 85 )
...
// etc
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 elseif
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.
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.
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
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.
if( number > 290 )
...
elseif( number > 125 )
...
elseif( 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)
}
elseif( 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