i have code here works pretty decent except when down 'processdata' function receive error
"the variable 'totalpay' being used without being initialized."
i've been working on couple hours , brain going numb, appreciated.
#include <iostream> #include <string> #include <climits> #include <iomanip> using namespace std; // prototypes int readint(string errormsg = "", int min = int_min, int max = int_max); void getdata(string&, int&, char&, double&); char getchoice(string prompt, char char1, char char2); double processdata(char service); void display(string, int, char, double); // regular service const double rcharge = 10.00; // regular service base rate first reg_mins. const double rpermin = 0.20; // regular service cost per minute on reg_minutes. const int reg_mins = 50; // regular service free minutes. // premium service const double pcharge = 25.00; // minimum charge premium service. const double p_per_min_day = 0.10; // charge per day minutes after 75. const double p_per_min_night = 0.05; // charge per night minutes after 100. const int p_day = 75; // number of free minutes allowed during day. const int p_night = 100; // number of free minutes allowed during night. int main() { string name; // stores users name. int accountnumber; // stores users account number. char servicetype; // stores users service type. double minutes; // stores users minutes. double totalpay; // recieves total pay processdata. bool loopies = true;// controls loop while condition true. while (loopies == true) { getdata(name, accountnumber, servicetype, minutes); totalpay = processdata(servicetype); display(name, accountnumber, servicetype, totalpay); } return 0; } // checks ints make sure valid. int readint(string errormsg, int min, int max) { int someint; bool valid = false; { // user entering account number cin >> someint; // verifying data valid (0 9999) valid = (someint >= min && someint <= max); // clearing out invalid data , prompting re-entry if (cin.fail() || !valid || !isspace(cin.peek())) { cout << "error! invalid input." << endl; cout << errormsg << " must whole number between " << min << " , " << max << endl; cout << "try again: "; cin.clear(); // clearing fail state cin.ignore(numeric_limits<streamsize>::max(), '\n'); valid = false; } } while (!valid); // clears out following incorrect data once correct cin.ignore(100, '\n'); return someint; } // takes user data , checks input. void getdata(string &name, int &account, char &service, double &bill) { cout << "enter customer name or \"exit\" end program: "; getline(cin, name); if (name == "exit" || name == "exit" || name == "exit") { cout << "program closing..." << endl; system("pause"); exit(exit_success); } cout << "enter account number: "; account = readint("account", 0, 10000); cout << "enter service type (r regular or p premium): "; service = getchoice("service", 'r', 'p' ); } char getchoice(string input, char char1, char char2) { char character; // stores users character. bool valid = false; // controls loop. { cin >> character; character = tolower(character); valid = (character == char1 || character == char2); if (!valid || !isspace(cin.peek())) { cout << "invalid input!" << endl; cout << input << " needs " << char1 << " or " << char2 << endl; cout << "try again: "; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); valid = false; } } while (!valid); cin.ignore(100, '\n'); return toupper(character); } double processdata(char service) { int daymin; // stores users day minutes. int nightmin; // stores users night minutes. double totalpay; // stores total pay. double daypay; // stores pay day minutes. double nightpay; // stores pay night minutes. if (service == 'r') { cout << "enter minutes used: "; cin >> daymin; daymin = readint("minutes ", 0, int_max); if (daymin > reg_mins) { daymin -= reg_mins; totalpay = (daymin * rpermin) + rcharge; } else { totalpay = rcharge; } totalpay = nightpay + daypay - rcharge; } else if (service == 'p') { cout << "enter day minutes: "; cin >> daymin; daymin = readint("minutes ", 0, int_max); if (daymin > p_day) { daymin -= p_day; daypay = (daymin * p_per_min_day) + pcharge; } else { daypay = pcharge; } cout << "enter night minutes: "; cin >> nightmin; nightmin = readint("minutes ", 0, int_max); if (nightmin > p_night) { nightmin -= p_night; nightpay = (nightmin * p_per_min_night) + pcharge; } else { nightpay = pcharge; } } return totalpay; } void display(string name, int account, char service, double bill) { string switchservice; switch (service) { case 'r': switchservice = "regular"; break; case 'p': switchservice = "premium"; break; default: cout << "invalid character."; } cout << fixed << setprecision(2); cout << endl; cout << "customer name: " << name << endl; cout << "account number: " << account << endl; cout << "service type: " << switchservice << endl; cout << "amount due: $" << bill << endl << endl; }
in processdata()
, if service
neither p
nor r
, totalpay
never assigned before returning.
you may initialize on declaration like
double totalpay = 0.0;
Comments
Post a Comment