4 Replies - 340 Views - Last Post: 09 July 2009 - 09:17 PM Rate Topic: -----

#1 gantis  Icon User is offline

  • New D.I.C Head

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

Help me with an If Statement

Post icon  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 amountDue, amountDuePremium; //total payment
double minutesDay, minutesNight;//consumed minutes for premium plan
double calcRegBill();
double calcPremBill(); 

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

cout << "Please enter your account number: ";
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  Icon User is offline

  • D.I.C Head

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

Re: Help me with an If Statement

Posted 08 July 2009 - 09:44 PM

huh :/
Was This Post Helpful? 0
  • +
  • -

#3 mblazek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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;


Was This Post Helpful? 0
  • +
  • -

#4 gantis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :rolleyes:

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

Was This Post Helpful? 0
  • +
  • -

#5 teh-JaR  Icon User is offline

  • New D.I.C Head

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

Re: Help me with an If Statement

Posted 09 July 2009 - 09:17 PM

View Postgantis, 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 :rolleyes:

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));
and instead of
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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1