Maximum PC

 It is currently Thu Apr 24, 2014 4:16 am

 All times are UTC - 8 hours

 Page 1 of 1 [ 11 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Need help with my home workPosted: Tue Sep 30, 2008 7:05 pm
 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.

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();
int main()
{

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')
else
{
cout<< "Wrong input, program terminated!" << endl;
return 1;
}

}
{

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

 Post subject: Posted: Wed Oct 01, 2008 3:06 am
 Monkey Federation (Top 10)*

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')
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

 Post subject: Posted: Wed Oct 01, 2008 7:26 am
 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

 Post subject: Posted: Wed Oct 01, 2008 7:45 am
 Monkey Federation (Top 10)*

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

 Post subject: Posted: Wed Oct 01, 2008 8:53 am
 SON OF A GUN

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

 Post subject: Posted: Wed Oct 01, 2008 9:21 am
 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

 Post subject: Posted: Wed Oct 01, 2008 9:57 am
 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

 Post subject: Posted: Wed Oct 01, 2008 10:27 am
 Monkey Federation (Top 10)*

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

 Post subject: Posted: Wed Oct 01, 2008 3:46 pm
 SON OF A GUN

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

 Post subject: Posted: Wed Oct 01, 2008 3:53 pm
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
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

 Post subject: Posted: Wed Oct 01, 2008 5:40 pm
 Monkey Federation (Top 10)*

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.

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 11 posts ]

 All times are UTC - 8 hours

Who is online

Users browsing this forum: No registered users and 4 guests

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

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets