# Help me with an If Statement

Page 1 of 1

## 4 Replies - 460 Views - Last Post: 09 July 2009 - 09:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=113807&amp;s=7f8e9b32eb740b5f604999c86e59bacb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gantis

Reputation: 0
• Posts: 21
• Joined: 20-March 09

# Help me with an If Statement

Posted 08 July 2009 - 09:01 PM

How do I calculate only the minutes used over 50 in the if statement of the function calcRegBill?

```#include<iostream>
#include<stdlib.h>
#include<string>
#include<iomanip>

using namespace std;
const double REG_BILL_SERV_COST=10.00;;
const double REG_BILL_OVER_MINS=.20;

const double PREM_BILL_SERV_COST=25.00;
const double PREM_BILL_DAY_MINS=75;
const double PREM_BILL_DAY_OVER_MINS=.10;
const double PREM_BILL_NIGHT_MINS=100;
const double PREM_BILL_NIGHT_OVER_MINS=.05;

int main()
{

char serviceCode; //service plan

double acctNum; //account number
double minutesDay, minutesNight;//consumed minutes for premium plan
double calcRegBill();
double calcPremBill();

cout << fixed << showpoint;
cout << setprecision(2);

cin >> acctNum;
cout << "\nEnter customer type, R, r (Regular), p, P (Premium): ";
cin >> serviceCode;

switch (serviceCode)
{
case 'r':
case 'R':
amountDue=calcRegBill();
cout << "Account number = " << acctNum << endl;
cout << "Amount due = \$" << amountDue << endl;
break;
default:
cout << "Invalid customer type" << endl;
}
system("pause");
return 0;
}

double calcRegBill()
{
int minutesUsed; //consumed minutes for regular plan
double bAmount; //bill amount

cout << "Enter the number of minutes used: ";
cin >> minutesUsed;
if(minutesUsed > 50)
bAmount=REG_BILL_SERV_COST + (REG_BILL_OVER_MINS*minutesUsed > 50);
else
bAmount=REG_BILL_SERV_COST + (REG_BILL_SERV_COST*minutesUsed);
cout << endl;

return bAmount;
}
```

This post has been edited by gantis: 09 July 2009 - 07:16 PM

Is This A Good Question/Topic? 0

## Replies To: Help me with an If Statement

### #2 Mowgef

Reputation: 11
• Posts: 245
• Joined: 01-May 09

## Re: Help me with an If Statement

Posted 08 July 2009 - 09:44 PM

huh

### #3 mblazek

Reputation: 1
• Posts: 19
• Joined: 05-March 09

## Re: Help me with an If Statement

Posted 08 July 2009 - 10:04 PM

I believe what you are looking to do is listed below:

```	   if(minutesUsed > 50)
bAmount=REG_BILL_SERV_COST + (REG_BILL_OVER_MINS*(minutesUsed - 50));
else
bAmount=REG_BILL_SERV_COST + (REG_BILL_SERV_COST*minutesUsed);
cout << endl;

```

### #4 gantis

Reputation: 0
• Posts: 21
• Joined: 20-March 09

## Re: Help me with an If Statement

Posted 09 July 2009 - 07:14 PM

How would I manipulate the algorithms so they don't add 'PREM_BILL_SERVICE_COST' (\$25) twice, when getting the total amount

```double calcPremBill()
{
int minutesDay;
int minutesNight;
double dayAmount;
double nightAmount;
double bAmount;

cout << "Enter the number of daytime minutes used (6 a.m. - 6 p.m.): ";
cin >> minutesDay;
cout << "Enter the numbre of nighttime minutes used (6 p.m. - 6 a.m.): ";
cin >> minutesNight;

if(minutesDay > 75)
dayAmount=PREM_BILL_SERV_COST + (PREM_BILL_DAY_MINS*(minutesDay-75));
else
dayAmount=PREM_BILL_SERV_COST;

if(minutesNight > 100)
nightAmount=PREM_BILL_SERV_COST + (PREM_BILL_NIGHT_MINS + (PREM_BILL_NIGHT_OVER_MINS * (minutesNight-100));
else
nightAmount=PREM_BILL_SERV_COST;

bAmount=dayAmount+nightAmount;

return bAmount;
}
```

This post has been edited by gantis: 09 July 2009 - 07:16 PM

### #5 teh-JaR

Reputation: 4
• Posts: 29
• Joined: 03-July 09

## Re: Help me with an If Statement

Posted 09 July 2009 - 09:17 PM

gantis, on 9 Jul, 2009 - 06:14 PM, said:

How would I manipulate the algorithms so they don't add 'PREM_BILL_SERVICE_COST' (\$25) twice, when getting the total amount

I think, there are a few bugs. I'm guessing, that instead of
dayAmount=PREM_BILL_SERV_COST + (PREM_BILL_DAY_MINS*(minutesDay-75));
you want to write
dayAmount=PREM_BILL_SERV_COST + (PREM_BILL_DAY_OVER_MINS * (minutesDay - PREM_BILL_DAY_MINS));
nightAmount=PREM_BILL_SERV_COST + (PREM_BILL_NIGHT_MINS + (PREM_BILL_NIGHT_OVER_MINS * (minutesNight-100));
you want to write
nightAmount=PREM_BILL_SERV_COST + (PREM_BILL_NIGHT_OVER_MINS * (minutesNight - PREM_BILL_NIGHT_MINS));

As to your question: That should really not be a problem, just delete the additions of PREM_BILL_SERV_COST at either the dayAmount assignments or the nightAmount assignments.
Actually, you can wrap the whole routine into four lines:
```double calcPremBill()
{
int minutesDay, minutesNight;
cout << "Enter the number of daytime minutes used (6 a.m. - 6 p.m.): "; cin >> minutesDay;
cout << "Enter the number of nighttime minutes used (6 p.m. - 6 a.m.): "; cin >> minutesNight;
return PREM_BILL_SERV_COST + (minutesDay > PREM_BILL_DAY_MINS ? PREM_BILL_DAY_OVER_MINS * (minutesDay - PREM_BILL_DAY_MINS) : 0) + (minutesNight > PREM_BILL_NIGHT_MINS ? PREM_BILL_NIGHT_OVER_MINS * (minutesNight - PREM_BILL_NIGHT_MINS) : 0);
}

```

Ok, the last line is kind of long, but you get the idea. If you don't know, how the ? : works: it's called the ternary conditional operator, look it up on cppreference or somewhere else, it can be very useful, but don't overuse it. (As you can see, it can lead to very very long expressions.)

Greetings,
teh-JaR