[VIEWED 5184
TIMES]
|
SAVE! for ease of future access.
|
|
|
TD
Please log in to subscribe to TD's postings.
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; }
|
|
|
|
himalayandude
Please log in to subscribe to himalayandude's postings.
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.......
|
|
|
himalayandude
Please log in to subscribe to himalayandude's postings.
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
|
|
|
n_nepal
Please log in to subscribe to n_nepal's postings.
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
|
|
|
thapap
Please log in to subscribe to thapap's postings.
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; }
|
|
|
mickthesick
Please log in to subscribe to mickthesick's postings.
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.
|
|
|
silent
Please log in to subscribe to silent's postings.
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.
|
|
|
thapap
Please log in to subscribe to thapap's postings.
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:
|
|
|
thapap
Please log in to subscribe to thapap's postings.
Posted on 11-21-06 12:32
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
|
|
|
silent
Please log in to subscribe to silent's postings.
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.
|
|
|
thapap
Please log in to subscribe to thapap's postings.
Posted on 11-21-06 1:25
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
|
|
|
thapap
Please log in to subscribe to thapap's postings.
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: ]
|
|