Function and selection issue in C++ -
i revised post. new programming , not know if functions set correctly or how allow selection of packages. below objective.
i have class assignment write c++ program calculate customers monthly internet bill. input customer name, package customer purchased, , how many hours used.
the requirements are:
input validation: sure user selects package b or c. display , error message if wrong package entered.
calculation:
packa 9.95 month 10 hours of acess. 2 bucks per additional hour. packb 14.95 month 20 hours of acess. 1 bucks per additional hour. packc 19.95 month unlimited acess.
i have create output bill.
the code wrote far is:
/* james hayek cis 165 passaic county community college program 04 */ #include <iostream> #include <iomanip> using namespace std; double calcbill(double packagechoosen, double hours, double basepack, char name); // function prototype calcbill void dispbill(char packagechoosen, double hours, double basepack, char name, char bill); // function prototype dispbill int main() { char packagechoosen; double bill; char name; double hours; double basepack; cout << "welcome personal bill caclculator!\n\n"; cout << "please enter name: \n"; cin >> name; cout << "please choose package entering a, b, or c: \n"; cin >> packagechoosen; bill = calcbill(name, packagechoosen, hours, basepack); // call function calcbill dispbill(name, packagechoosen, hours, basepack, bill); // call function dispbill return 0; } // end main double calcbill(char packagechoosen, double hours, double basepack, char name) //this function calcbill { if (packagechoosen == 'a' || packagechoosen == 'a' && hours <= 10) { bill = 9.95; } else if (packagechoosen == 'a' || packagechoosen == 'a' && hours > 10) { bill = 9.95 + (2 * hours); } else { cout << "please choose apropriate package"; } return bill; } // end main void dispbill(double packa, double packb, double packc, char name, char bill) //this function dispbill { cout << bill; return; } // end dispbill
i not sure begin, advice appreciated.
the &&
operator binds more tightly ||
operator in c++. rather than
packagechoosen == 'a' || packagechoosen == 'a' && hours > 10
as boolean expression, want
(packagechoosen == 'a' || packagechoosen == 'a') && hours > 10
the top expression looks either "package a" or "package hours > 10". bottom expression looks "package or package a" , "hours > 10".
as platinum azure mentions in answer, possible convert whatever character have lowercase, single comparison against lowercase letter.
#include <cctype> // ... std::tolower(packagechoosen) == 'a' && hours > 10
this makes code little more readable, , don't have worry operator precedence.
Comments
Post a Comment