Quantcast

Maximum PC

It is currently Mon Sep 22, 2014 3:08 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Need help with my home work
PostPosted: Tue Sep 30, 2008 7:05 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
This is what i have to do:

Assignment 2 Cellular Phone Bill

Write a program that calculates and prints the bill for a cellular telephone company. The company offers two types of services: regular and premium. Its rates vary depending on the type of service. The rates are as follows:

Regular service: $10.00 plus first 50 minutes are free. Charges for over 50 minutes are $.20 pre minutes.

Premium service: $25.00 plus:

1. for calls made from 6:00am to 6:00pm, the first 75 minutes are free; charges for over 75 minutes are $.10 per minutes
2. for calls made from 6:00pm to 6:00am, the first 100 minutes are free; charges for over 100 minutes are $0.05 per minutes.

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

For 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 number of minutes the service was used during the day and the number of minutes the service was used during the night.

The output should be nicely formatted. The amount due should be output with a '$' sign before the number and the number should be displayed with two digits after decimal point. ( for example, $45.50)

Now this is my code:

Code:
#include <iostream>
#include <iomanip>
using namespace std;
int account_number;
float minutes;
char service;
float amount_due;
float day_min;
float night_min;
float regular();
float premium();
int main()
{
   
   cout<< "Please enter your account number: " ;
   cin >> account_number;
   cout<< "Do you have Regular service or Premium service (r or p)? ";
    cin.ignore(0,'\n') >> service;
   cout<< service;
   if (service == 'r' || 'R')
      regular();
   else if (service == 'p' || 'P')
      premium();
   else
   {
      cout<< "Wrong input, program terminated!" << endl;
      return 1;
   }
   
}
float premium()
{
   
         cout<< "How many minutes were used during the day? ";
          cin >> day_min;
          cout<< "How many minutes were used during the night? ";
          cin >>night_min;
   
   if (day_min <= 75)
   {
      amount_due = 25.00;
      cout<< fixed << showpoint << setprecision(2);
      cout<< "Amount due: $" << amount_due << endl;
      cout<< "Account number: "<< account_number <<endl;
      cout<< "Service type: "<< service << endl;
      cout<< "Minutes used: "<< day_min <<endl;
   }
   else if (day_min > 75)
   {
      amount_due = ((day_min - 75) * .10)+ 25.00;
      cout<< fixed << showpoint << setprecision(2);
      cout<< "Amount due: $" << amount_due << endl;
      cout<< "Account number: "<< account_number <<endl;
      cout<< "Service type: "<< service << endl;
      cout<< "Minutes used: "<< day_min <<endl;
   }
   else if (night_min <= 100)
   {
      amount_due = 25.00;
      cout<< fixed << showpoint << setprecision(2);
      cout<< "Amount due: $" << amount_due << endl;
      cout<< "Account number: "<< account_number <<endl;
      cout<< "Service type: "<< service << endl;
      cout<< "Minutes used: "<< day_min <<endl;
   }
   else if (night_min > 100)
   {
      amount_due = ((day_min - 100) * .05)+ 25.00;
      cout<< fixed << showpoint << setprecision(2);
      cout<< "Amount due: $" << amount_due << endl;
      cout<< "Account number: "<< account_number <<endl;
      cout<< "Service type: "<< service << endl;
      cout<< "Minutes used: "<< day_min <<endl;
   }
   return 0;
}
float regular ()
{
   cout<< "How many minutes were used? ";
   cin >> minutes;
   if (minutes <= 50.00)
   {
       amount_due = 10.00;
      cout<< fixed << showpoint << setprecision(2);
      cout<< "Amount due: $" << amount_due << endl;
      cout<< "Account number: "<< account_number <<endl;
      cout<< "Service type: "<< service << endl;
      cout<< "Minutes used: "<< minutes <<endl;
      
   }
   else
   {
       amount_due = ((minutes -50.00) * 0.20);
      cout<< setw(2) << showpoint << setprecision(3)<< "Amount due: $"<< amount_due << endl;
      cout<< "Account number: "<< account_number <<endl;
      cout<< "Service type: "<< service << endl;
      cout<< "Minutes used: "<< minutes <<endl;
   }
   return 0;
   
}


I want my program to terminate when someone types in a letter other than r, R, p, P. I am having trouble doing that any help please.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 3:06 am 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
If you really want to simplify this part:
Code:
   if (service == 'r' || 'R')
      regular();
   else if (service == 'p' || 'P')
      premium();
   else
   {
      cout<< "Wrong input, program terminated!" << endl;
      return 1;
   }
   


Use a switch/case block. When you look at the syntax you will see where you can place the errors for invalid characters. I am assuming this is a 100 level c++ course? That would assume you have not been introduced to objects yet?


Top
  Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 7:26 am 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
we havent learn switch/case block. Can you spot anything wrong with my code?


Top
  Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 7:45 am 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
xxmainframexpxx wrote:
we havent learn switch/case block. Can you spot anything wrong with my code?


Your if statement I highlighted has issues. You have a syntax issue that is preventing the program from doing what you think it should.

This is homework so no one is going to give you the answer; only point you in the right direction.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 8:53 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Look into toupper and tolower statements. Using one or the other of these will help you fix your IF block.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 9:21 am 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
i cant use those toupper and tolower functions; i havent learn that


Top
  Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 9:57 am 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
yay i got it. It should be

if (service == 'r' || service == 'R')


Top
  Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 10:27 am 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
xxmainframexpxx wrote:
yay i got it. It should be

if (service == 'r' || service == 'R')


ding ding ding!


Top
  Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 3:46 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
xxmainframexpxx wrote:
i cant use those toupper and tolower functions; i havent learn that
If your teacher is going to penalize you for figuring out a better and more elegant solution, then they are a tool bag.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 3:53 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
CrashTECH wrote:
xxmainframexpxx wrote:
i cant use those toupper and tolower functions; i havent learn that
If your teacher is going to penalize you for figuring out a better and more elegant solution, then they are a tool bag.


I agree.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 5:40 pm 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
CrashTECH wrote:
xxmainframexpxx wrote:
i cant use those toupper and tolower functions; i havent learn that
If your teacher is going to penalize you for figuring out a better and more elegant solution, then they are a tool bag.


but he did not figure it out, you told him about it. :P

Using a switch/case statement would look like this
Code:
switch(service)
{
   case 'p':
   case 'P': call_function_here();
            break;
  default: call_some_error_message();
}


This way since there is no break after the first case it falls to the next one. So if either an upper or lower is entered they both end up executing the same code.

Or, as Crash suggested, you could have modified the if statement to read
Code:
if(tolower(service) == 'p')
etc
etc...


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group