[Show all top banners]

TD
Replies to this thread:

More by TD
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 C Programing question
[VIEWED 5659 TIMES]
SAVE! for ease of future access.
Posted on 11-20-06 7:27 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

can you guys please tell me what in the heck is wrong with this program .... doesn't run
.....i tried to calculate cell phone bill .

#include
#include
using namespace std;

const double rServiceCost = 10.00;
const double rPerMinuteCharge = .20;
const double pServiceCost = 25.00;
const double pDayPerMinuteCharge = .10;
const double pNightPerMinuteCharge = .05;

int main()
{

int minutes;
int dayMinutes;
int nightMinutes;
char serviceCode;
int accountNumber;
double amountDue;

cout << "Enter an account number: ";
cin >> accountNumber;
cout << endl;

cout << "Enter a service code: R or r (regular), P or p (premium): ";
cin >> serviceCode;
cout << endl;

switch (serviceCode)
{
case 'r':
case 'R': cout << "Enter the number of minutes used: ";
cin >> minutes;
cout << endl;

if (minutes <= 50)
amountDue = rServiceCost;
else
amountDue = rServiceCost + ((minutes - 50) * rPerMinuteCharge);

cout << "Account number = " << accountNumber << endl;
cout << "Type of service: " << serviceCode << endl;
cout << "Number of minutes used: " << minutes << endl;
cout << "Amount Due = $" << amountDue << endl;
break;

case 'p':
case 'P': cout << "Enter the number of day minutes used: ";
cin >> dayMinutes;
cout << endl;
cout << "Enter the number of night minutes used: ";
cin >> nightMinutes;
cout << endl;

if (dayMinutes <= 75)
amountDue = pServiceCost;
else
amountDue = pServiceCost + ((minutes - 75) * pDayPerMinuteCharge);

cout << "Account number = " << accountNumber << endl;
cout << "Type of service: " << serviceCode << endl;
cout << "Number of minutes used: " << minutes << endl;
cout << "Amount Due = $" << amountDue << endl;

if (nightMinutes <= 100)
amountDue = pServiceCost;

else
amountDue = pServiceCost + ((minutes - 100) * pNightPerMinuteCharge);

cout << "Account number = " << accountNumber << endl;
cout << "Type of service: " << serviceCode << endl;
cout << "Number of minutes used: " << minutes << endl;
cout << "Amount Due = $" << amountDue << endl;
break;

default: cout << "Invalid service code" << endl;
}
return 0;
}
 
Posted on 11-20-06 8:38 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

// Program:
// Author: ***********
// Date: Nov 20 2006
// Assignment:
// Purpose:
//
// Input:
// Output: :
// Related
// Files:
// Functions:
//

#include

using namespace std;

const double rServiceCost = 10.00;
const double rPerMinuteCharge = .20;
const double pServiceCost = 25.00;
const double pDayPerMinuteCharge = .10;
const double pNightPerMinuteCharge = .05;

int main()
{

int minutes;
int dayMinutes;
int nightMinutes;
char serviceCode;
int accountNumber;
double amountDue;

cout << "Enter an account number: ";
cin >> accountNumber;
cout << endl;

cout << "Enter a service code: R or r (regular), P or p (premium): ";
cin >> serviceCode;
cout << endl;

switch (serviceCode)
{
case 'r':
case 'R': cout << "Enter the number of minutes used: ";
cin >> minutes;
cout << endl;

if (minutes <= 50)
amountDue = rServiceCost;
else
amountDue = rServiceCost + ((minutes - 50) * rPerMinuteCharge);

cout << "Account number = " << accountNumber << endl;
cout << "Type of service: " << serviceCode << endl;
cout << "Number of minutes used: " << minutes << endl;
cout << "Amount Due = $" << amountDue << endl;
break;

case 'p':
case 'P': cout << "Enter the number of day minutes used: ";
cin >> dayMinutes;
cout << endl;
cout << "Enter the number of night minutes used: ";
cin >> nightMinutes;
cout << endl;

if (dayMinutes <= 75)
amountDue = pServiceCost;
else
amountDue = pServiceCost + ((minutes - 75) * pDayPerMinuteCharge);

cout << "Account number = " << accountNumber << endl;
cout << "Type of service: " << serviceCode << endl;
cout << "Number of minutes used: " << minutes << endl;
cout << "Amount Due = $" << amountDue << endl;

if (nightMinutes <= 100)
amountDue = pServiceCost;

else
amountDue = pServiceCost + ((minutes - 100) * pNightPerMinuteCharge);

cout << "Account number = " << accountNumber << endl;
cout << "Type of service: " << serviceCode << endl;
cout << "Number of minutes used: " << minutes << endl;
cout << "Amount Due = $" << amountDue << endl;
break;

default: cout << "Invalid service code" << endl;
}
return 0;
}

I just copied what you had and named it test.cpp and compiled using g++ test.cpp . It runs fine.......


 
Posted on 11-20-06 8:39 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

By the way include only iostream on first line
 
Posted on 11-20-06 11:50 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

what you are trying to include....

there is only a key word "include"....you need to define what liberary file do you want to include.......

exp:

#include
 
Posted on 11-21-06 11:47 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

TD,
here is the answer to your question (O:
you need to start writing better code. create some function taking arguments thus you do not have to handle those double print statements.

also check the logic. you cannot double charge the customer (O:

#include
using namespace std;
const double rServiceCost = 10.00;
const double rPerMinuteCharge = .20;
const double pServiceCost = 25.00;
const double pDayPerMinuteCharge = .10;
const double pNightPerMinuteCharge = .05;


void display_info ( int accountNumber, char serviceCode, int minutes, double amountDue ) ;


int main()
{
int minutes;
int dayMinutes;
int nightMinutes;
char serviceCode;
int accountNumber;
double amountDue;
double nightTimeDue, dayTimeDue;

cout << "Enter an account number: ";
cin >> accountNumber;
cout << endl;
cout << "Enter a service code: R or r (regular), P or p (premium): ";
cin >> serviceCode;
cout << endl;

switch (serviceCode)
{
case 'r':
case 'R': cout << "Enter the number of minutes used: ";
cin >> minutes;
cout << endl;
if (minutes <= 50)
{
amountDue = rServiceCost;
}
else
{
amountDue = rServiceCost + ((minutes - 50) * rPerMinuteCharge);
}

display_info ( accountNumber, serviceCode, minutes, amountDue);

break;

case 'p':
case 'P': cout << "Enter the number of day minutes used: ";
cin >> dayMinutes;
cout << endl;
cout << "Enter the number of night minutes used: ";
cin >> nightMinutes;
cout << endl;
if (dayMinutes <= 75)
{
dayTimeDue = 0.00;
}
else
{
dayTimeDue = ((dayMinutes - 75) * pDayPerMinuteCharge);
}
if (nightMinutes <= 100)
{
nightTimeDue = 0;
}
else
{
nightTimeDue = ((nightMinutes - 100) * pNightPerMinuteCharge);
}
//since you have nightTime and dayTime you cannot charge twice "pServiceCost
// thus
{
amountDue = dayTimeDue + nightTimeDue + pServiceCost;
minutes = dayMinutes + nightMinutes ;
}
display_info ( accountNumber, serviceCode, minutes, amountDue);
break;
default: cout << "Invalid service code" << endl;
}
return 0;
}

void display_info ( int accountNumber, char serviceCode, int minutes, double amountDue )
{
cout << "Account number = " << accountNumber << endl;
cout << "Type of service: " << serviceCode << endl;
cout << "Number of minutes used: " << minutes << endl;
cout << "Amount Due = $" << amountDue << endl;
}
 
Posted on 11-21-06 12:05 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

/Thapap, from the look of TD's program, I can tell for sure that he has not started learning the "void" functions. So don't confuse him more by using the void function.

And TD, your logic on the premium charge is flawed, just like Thapap pointed out. You are ripping the customer off by charging him double.

Amount Due for premium service should be = minimum charge + daytime extra charge + nighttime extra charge.

So on your code for daytime minutes, you should have:

if (dayMinutes <= 75)
daytime extra charge = 0;
else
daytime extra charge= (minutes - 75) * pDayPerMinuteCharge;


Also your code on night time minutes should be:

if (nightMinutes <= 100)
night time extra charge = 0;
else
night time extra charge= (minutes - 100) * pNightPerMinuteCharge;

Therefore your total amount due for premium service is:

amountDue = minimum premium cahrge + daytime extra charge + nighttime extra charge.
 
Posted on 11-21-06 12:06 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

thapap, is it a good idea to create functions for small tasks as printing few values? Doesnt the overhead associated with creating a new stack frame for such a small task outwiegh the simplicity of inlining ? may be its okay for the small program as the one above. but for large programs, i think its better to inline such small tasks instead of using a function.
 
Posted on 11-21-06 12:29 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

silent jyu,

Keyword "inline" "function" or not.
here is a read for you.
if you want to cut and paste (O:
http://www.velocityreviews.com/forums/t267972-inline-or-not-to-inline-in-c.html

i can do this as well can't I
inline void display_info ( int accountNumber, char serviceCode, int minutes, double amountDue )
{
}

will this make you happy (O:

====================================================
as always,
what do i know (O:
 
Posted on 11-21-06 12:32 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

silent jyu,

Keyword "inline" "function" or not.
here is a View/Share this post only
 
Posted on 11-21-06 12:56 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

yes u can use inline keyword to inline a function. But I remember my compiler course where we discussed which one is better - actual inling or using the inline keyword . My professor , as he was a compiler person, wanted the former coz u dont want to make compiler do extra stufff which otherwise u as a programmer can do.
 
Posted on 11-21-06 1:25 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 
 
Posted on 11-21-06 1:33 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

silent jyu,
did you have a chance to read this one:

http://www.velocityreviews.com/forums/t267972-inline-or-not-to-inline-in-c.html
.

yes i very well understand the concept and its usages. but as in any programming there are trade offs and real life is a lot different than actual academia.

every aspect of programming has its own preference.

hence while writing compilers one is concerned about speed. thus inlining functions would reduce its performance. that is why if you must inline i.e. try to inline each lines for the optimal optimization and performance in those. if a must one would try to inline smaller or simpler functions.

but when you are writing applications yes speed is a must but we can forgo that in terms of ease of use. and readability. i am not going to type that printf for 12 times if i can go away with doing it 4.


[ i hope you understand what inline means to compilers and how they handle it(O: ]
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 30 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
ढ्याउ गर्दा दसैँको खसी गनाउच
To Sajha admin
From Trump “I will revoke TPS, and deport them back to their country.”
and it begins - on Day 1 Trump will begin operations to deport millions of undocumented immigrants
Travel Document for TPS (approved)
All the Qatar ailines from Nepal canceled to USA
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters