12 Replies - 7779 Views - Last Post: 13 November 2008 - 10:48 PM Rate Topic: -----

#1 bori_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 03-October 08

Program that calculates and prints a bill for a cell phone company

Post icon  Posted 09 October 2008 - 04:04 PM

This is my next assignemnt.
It was to write a program that calculates and prints the bill for a cellular phoen company. The company offers 2 types of service: regular and premium. Its rates vary, depending on the type of service. The rates are computes as follows:

Regular service:

$10.00 + 1st 50 minutes are free. Charges for over 50 minutes are $.20 per minute.

Premium service:

$25.00 plus:

a: For calls made from 6:00 a.m. to 6p.m., the 1st 75 minutes are free; charges for over 75 minutes are $.10 per minute.
b: For calls made from 6:00 p.m. to 6:00 a.m., the 1st 100 minutes are free; for over 100 minutes are $.50 per minute.

Your program should prompt the user to enter an account#, a service code (type char) and the # of minutes the service was used. A service code of r or R means regular service; a service code for p or P means premium service. Treat any other character as an error. Your program should output the account#, type of service, # of minutes the phone service was used, and the amount due from the user.

For the premium service, the customer may be using the service during the day and the night. Therefore to calculate the bill you must ask the user to input the # of minutes the service was used during the day and the # of minutes the service was used during the night.

That was an assignment which I did not get and did not work. Now the current assignment is asking to rewrite it so that it does not output the # of minutes during which the service used.

a: regular bill: this function calculates and returns the billing amount for regular service.

b: premium bill: this function calculates and returns the billing amount for premium service.

I have some parts from the last program but I was and still confused on what to do. I am trying to revise it but I need help or an example to show me what I am doing.

Is This A Good Question/Topic? 0
  • +

Replies To: Program that calculates and prints a bill for a cell phone company

#2 Alianin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-October 08

Re: Program that calculates and prints a bill for a cell phone company

Posted 09 October 2008 - 04:13 PM

Why don't you post what you do have so we can help you understand it better?
Was This Post Helpful? 0
  • +
  • -

#3 bori_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 03-October 08

Re: Program that calculates and prints a bill for a cell phone company

Posted 09 October 2008 - 04:31 PM

View PostAlianin, on 9 Oct, 2008 - 04:13 PM, said:

Why don't you post what you do have so we can help you understand it better?


Here is some of it. I am stiil trying to figure out the rest and at the same time fixing whatever seems wrong to me.
#
include <>
#include <iostream>


using namespace std;

const double REG_CHARGES = 10.00;
const int REG_MINUTES = 50;
const double REG_RATE_OVER_50 = 0.20;

const double PREM_SERV_CHARGES = 25.00;
const int PREM_FREE_DAY_MINUTES = 75;
const double PREM_DAY_RATE_OVER_75 = 0.10;

const int PREM_FREE_NIGHT_MINUTES = 100;
const double PREM_NIGHT_RATE_OVER_100 = 0.05;

double regular();
double premium ();

int main(void)
{
      
      //Variable Declaration
  int account,minutes;
  char service_type;
  double amount_due=0
  
  cout << fixed << showpoint;
  cout << setprecision(2);
  
  cout << "Enter Account Number" << endl;
  cin >> account;
  
  cout << "Enter Service Type: "
      << "R or r (Regular Service), "
      << "P or p (Premium Service): ";
    cin>>service_type;
    
  cout << "Enter Number of Minutes"<<endl;
     cin >> minutes;
  while (minutes<0)
  {
     cout << "Enter Number of Minutes" << endl;
     cin >> minutes;
  }
  
  switch (service_type)
  {
  case 'r':
  case 'R':
            
       amount = regular();
       cout << " Account Number = "
            << account<< endl;
       cout << Amount Due = $"
            << amount_due << end;
       break;          
  case 'p':
  case 'P':
       amount = premium();
       cout << "Account number = "
            << account << end;
       cout << "Amount due = $"
            << amount_due << end;
       break;                 
  default:
       cout << "Invalid customer type."
            << endl;
  }
  return 0;
}

double regular()
{ 
       int                    

** Edit ** :code:
Was This Post Helpful? 0
  • +
  • -

#4 bori_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 03-October 08

Re: Program that calculates and prints a bill for a cell phone company

Posted 09 October 2008 - 05:01 PM

here's the rest of my guess.If some one can look at it and give me some pointers or direction I will GREATLY appreciate it!!!


double regular()
{ 
	   double bAmount;
	   cout << "Enter Number of Minutes"<<endl;
	 cin >> minutes;
  while (minutes<0)
  {
	 cout << "Enter Number of Minutes" << endl;
	 cin >> minutes;
  }
	 if (minutes <= REG_MINUTES)
			amount_due = REG_CHARGES;
		else
			amount_due = REG_CHARGES 
						+ (minutes - REG_MINUTES) 
						  * REG_RATE_OVER_50;
			cout << "Account Number: " << account << endl;
			cout << "Service Type: Regular" << endl;
			cout << "Amount Due: $" << amount_due << endl;
			
			return bAmount;
}

double premium()
{
	   int day_minutes,night_minutes;
	   double bAmount;
		  cout << "Enter day time minutes used: ";
		cin >> day_minutes;
		cout << endl;

		cout << "Enter night time minutes used: ";
		cin >> night_minutes;
		cout << endl;

		bAmount = PREM_SERV_CHARGES;

		if (day_minutes > PREM_FREE_DAY_MINUTES)
			bAmount = bAmount + 
						(day_minutes - PREM_FREE_DAY_MINUTES) 
						 * PREM_DAY_RATE_OVER_75;

		if (night_minutes > PREM_FREE_NIGHT_MINUTES)
			bAmount = bAmount + 
						(night_minutes - PREM_FREE_NIGHT_MINUTES)
						  * PREM_NIGHT_RATE_OVER_100;

		cout << "Account Number: " << account << endl;
		cout << "Service Type: Premium" << endl;
		cout << "Minutes Service Used (Day): " 
			 << day_minutes << endl;
		cout << "Minutes Service Used (Night): " 
			 << night_minutes << endl;
		return bAmount;
}		
		   
					  
 default: cout << "Invalid Service Type." << endl;
	 cout << "Enter service type: 'r' or 'R' for Regular, or 'p' or 'P' for Premium: ";
	 cin >> service;
	 cout << endl;
} // end switch

cin.ignore();//causes dos screen to pause
return 0;
	 
}

Was This Post Helpful? 0
  • +
  • -

#5 Logomachist  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 74
  • Joined: 10-March 08

Re: Program that calculates and prints a bill for a cell phone company

Posted 09 October 2008 - 07:34 PM

To start out with, I needed to make the following changes to get the code to compile:
  • Added a break on line 69 for readability
  • Missing a semi-colon at the end of the amount_due initialization
  • To use setprecision() you must include iomanip
  • In case R (and again in P) you need use a variable amount- I assume this is supposed to be amount_due ?
  • Also missing a " in the same case
  • In case P you have a couple "end"s that should be "endl"s
  • You solicit the number of minutes in the main function and then you solicit it again in regular(); Is that correct? If so you need to declare minutes in regular() too. Otherwise you need to pass minutes to regular as a function parameter.
  • Same goes for amount_due
  • And account
  • And account in premium
  • You have a default: case thown in at the end of the file which I assume is a mistake? And an unneeded return 0;

Ok now it compiles.

Problem, after entering an account number the user doesn't get a chance to enter a service type because cin<< service_type eats up the return at the end of the account number. You need to eat the return before you ask for the service type.

You need to look at your program logic. You're asking for minutes in multiple places, you're outputting the account number in regular() and in main(). This results in your doing things more times than I think you're supposed to. I suggest drawing a basic flowchart for your program- that should clear things up in your head and allow you to easily identify where you want to be doing what. You can do it on paper or on the computer; if you want to do it on the computer I'd recommend <a href="http://sourceforge.net/projects/devflowcharter/">Devflowcharter</a>, a cute little program that allows you to draw flowcharts and convert them into code. It can generate C code, but not C++ code so you'd probably need to change things in a few places if you wanted to use the code it generated, and if you don't know what you're doing that might confuse you. But the flowchart view should work fine and dandy and help you realize your mistakes.
Was This Post Helpful? 0
  • +
  • -

#6 bori_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 03-October 08

Re: Program that calculates and prints a bill for a cell phone company

Posted 12 October 2008 - 04:29 PM

ok i did some adjustments but i still have 4 errors. it saids that my minutes and account are undeclared. I don't get it. I declared it in the beginning
int account; and int minutes; .
what is it that i did wrong? i think once i this is fixed i can see if my program works or not or what adjustments i need to make.
Was This Post Helpful? 0
  • +
  • -

#7 bori_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 03-October 08

Re: Program that calculates and prints a bill for a cell phone company

Posted 12 October 2008 - 04:40 PM

ok i got rid of my errors but when i try 2 run it i cant seem to see it because it goes to quick. i have this statement before the return:
cin.ignore();//causes dos screen to pause
but how can i have it to stay a couple minutes longer that i can view the output to see if i did the program right?
Was This Post Helpful? 0
  • +
  • -

#8 bori_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 03-October 08

Re: Program that calculates and prints a bill for a cell phone company

Posted 12 October 2008 - 04:54 PM

this is my program now:

#include <iostream>
#include <iomanip>

using namespace std;

const double REG_CHARGES = 10.00;
const int REG_MINUTES = 50;
const double REG_RATE_OVER_50 = 0.20;

const double PREM_SERV_CHARGES = 25.00;
const int PREM_FREE_DAY_MINUTES = 75;
const double PREM_DAY_RATE_OVER_75 = 0.10;

const int PREM_FREE_NIGHT_MINUTES = 100;
const double PREM_NIGHT_RATE_OVER_100 = 0.05;

double regular();
double premium ();

int main(void)
{
	   	
	  //Variable Declaration
  int account;
  
  char service_type;
  double amount_due=0;
  
  cout << fixed << showpoint;
  cout << setprecision(2);
  
  cout << "Enter Account Number" << endl;
  cin >> account;
  
  cout << "Enter Service Type: "
	  << "R or r (Regular Service), "
	  << "P or p (Premium Service): ";
	cin >> service_type;
	  
  switch (service_type)
  {
  case 'r':
  case 'R':
			
	   amount_due = regular();
	   cout << " Account Number = "
			<< account << endl;
	   cout << "Amount Due = $"
			<< amount_due << endl;
	   break;		  
  case 'p':
  case 'P':
	   
	   amount_due = premium();
	   cout << "Account number = "
			<< account << endl;
	   cout << "Amount due = $"
			<< amount_due << endl;
	   break;				 
  default:
	   cout << "Invalid customer type."
			<< endl;
  }
  return 0;
}

double regular()
{ 
	   int minutes;
	   double bAmount;
	   
	   cout << "Enter Number of Minutes" << endl;
	   cin >> minutes;
	   
  while (minutes<0)
  {
	 cout << "Enter Number of Minutes" << endl;
	 cin >> minutes;
  }
	 if (minutes <= REG_MINUTES)
			bAmount = REG_CHARGES;
		else
			bAmount = REG_CHARGES 
						+ (minutes - REG_MINUTES) 
						  * REG_RATE_OVER_50;
			cout << "Service Type: Regular" << endl;
			cout << "Amount Due: $" << bAmount << endl;
			
			return bAmount;
}

double premium()
{
	   int day_minutes,night_minutes;
	   double bAmount;
	   
		  cout << "Enter day time minutes used: ";
		cin >> day_minutes;
		cout << endl;

		cout << "Enter night time minutes used: ";
		cin >> night_minutes;
		cout << endl;

		bAmount = PREM_SERV_CHARGES;

		if (day_minutes > PREM_FREE_DAY_MINUTES)
			bAmount = bAmount + 
						(day_minutes - PREM_FREE_DAY_MINUTES) 
						 * PREM_DAY_RATE_OVER_75;

		if (night_minutes > PREM_FREE_NIGHT_MINUTES)
			bAmount = bAmount + 
						(night_minutes - PREM_FREE_NIGHT_MINUTES)
						  * PREM_NIGHT_RATE_OVER_100;

		cout << "Service Type: Premium" << endl;
		cout << "Minutes Service Used (Day): " 
			 << day_minutes << endl;
		cout << "Minutes Service Used (Night): " 
			 << night_minutes << endl;

			
		return bAmount;
		 
}		




the output should be
Enter account number: 56789
Enter service type: (r or R) for regular (p or P) for premium
service:
p
Enter day time minutes used: 88
Enter night time minutes used: 97
Account Number: 56789
Service Type: Premium
Amount Due: $26.30
Press any key to continue . . .

Enter account number: 23456
Enter service type: (r or R) for regular (p or P) for premium
service:
r
Enter number of minutes service used: 20
Account Number: 23456
Service Type: Regular
Amount Due: $10.00
Press any key to continue . . .


i cant check it because the output goes by to quick. can anyone advice me to what to do next so i may see if its right?

This post has been edited by bori_77: 13 October 2008 - 06:47 AM

Was This Post Helpful? 0
  • +
  • -

#9 bori_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 03-October 08

Re: Program that calculates and prints a bill for a cell phone company

Posted 13 October 2008 - 12:25 PM

I got it to work. the only thing i have is what do i put or what can i do that when the person inputs what service type they have(premium or regular) and they input a different letter instead of r, R, or p, P that the computer will tell them invalid input and for them to input the correct one.
Was This Post Helpful? 0
  • +
  • -

#10 bori_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 03-October 08

Re: Program that calculates and prints a bill for a cell phone company

Posted 13 October 2008 - 01:19 PM

i have an error that states that the account is undeclared. this is when i input in the double statements? i dont know why its giving me this error. can someone help me in understanding why and what the problem is?
Was This Post Helpful? 0
  • +
  • -

#11 Logomachist  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 74
  • Joined: 10-March 08

Re: Program that calculates and prints a bill for a cell phone company

Posted 14 October 2008 - 02:57 AM

View Postbori_77, on 13 Oct, 2008 - 12:25 PM, said:

I got it to work. the only thing i have is what do i put or what can i do that when the person inputs what service type they have(premium or regular) and they input a different letter instead of r, R, or p, P that the computer will tell them invalid input and for them to input the correct one.


You need to solicit the input in a loop that only exits if the input is as expected.

View Postbori_77, on 13 Oct, 2008 - 01:19 PM, said:

i have an error that states that the account is undeclared. this is when i input in the double statements? i dont know why its giving me this error. can someone help me in understanding why and what the problem is?


Can't help you with this one. <a href="http://www.dreamincode.net/forums/index.php?showtopic=66955&st=0&#">The program</a> is compiling fine for me w/ gcc on WinXP.
Was This Post Helpful? 0
  • +
  • -

#12 bori_77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 03-October 08

Re: Program that calculates and prints a bill for a cell phone company

Posted 12 November 2008 - 03:36 PM

Ok, this assignment I am supposed to redo this program so that all of the named constants are defined in a namespace. I don't understand what to do and how I use namespace. Can someone explain this a little further and direct me on how I go about redoing this program?
Thanx
Was This Post Helpful? 0
  • +
  • -

#13 Logomachist  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 74
  • Joined: 10-March 08

Re: Program that calculates and prints a bill for a cell phone company

Posted 13 November 2008 - 10:48 PM

All you need to do is to define a namespace like this:

namespace namespace_name_you_get_to_pick
{

};



And then move the const variables you already defined into the namespace. Then when you want to reference the variables in your program, you prefix them with the namespace name and two colons like so:

namespace_name_you_get_to_pick::variable_name 



You can also use a namespace directive if you're only using a single namespace in your program. That looks like:

using namespace namespace_name_you_picked;



...Which is what you've been doing up until now with std. And that's all there is to it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1