### Function logic error

Hello everyone,

Do you mind checking my whole coding below, the program is purposely created to compute the cost of the call when certain call is made during certain period on certain day. The function `void EnterTime` does not run well. I was trying to use `char hour[3] and char mins[3]` to read two numbers as hour (HH) and minutes (MM) and convert them to integer. I can't enter time. No syntax error, but seems like logic error might be the reason.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127`` ``````#include #include #include using namespace std; void EnterDay (char& day, char& day2) { do { cout << "Enter day : "; cin >> day >> day2; day = toupper(day); day2 = toupper(day2); if ((day != 'M' || day2 != 'O') && (day != 'T' || day2 != 'U') && (day != 'W' || day2 != 'E') && (day != 'T' || day2 != 'H') && (day != 'F' || day2 != 'R') && (day != 'S' || day2 != 'A') && (day != 'S' || day2 != 'U')) cout << "Wrong input!\nYou may try again\n" << endl; }while((day != 'M' || day2 != 'O') && (day != 'T' || day2 != 'U') && (day != 'W' || day2 != 'E') && (day != 'T' || day2 != 'H') && (day != 'F' || day2 != 'R') && (day != 'S' || day2 != 'A') && (day != 'S' || day2 != 'U')); } void TestCost(int& duration, int& num, double& call_cost, char& day, char& day2) { double billrate1 = 0.40, billrate2 = 0.25, billrate3 = 0.15; do { cout << "\nEnter num : "; cin >> num; cout << "\nEnter duration (minutes) : "; cin >> duration; if ((day != 'S' && day2 != 'A') && (day != 'S' && day2 != 'U') && ((num > 0) && (num < 50)) && (duration > 0)){ call_cost = duration * billrate1; cout.precision(2); cout.setf(ios::fixed); cout << "The cost of the call : \$" << call_cost; } else if ((day != 'S' && day2 != 'A') && (day != 'S' && day2 != 'U') && ((num >= 50) && (num <= 100)) && (duration > 0)){ call_cost = duration * billrate2; cout.precision(2); cout.setf(ios::fixed); cout << "The cost of the call : \$" << call_cost; } else if ((day == 'S' && day2 == 'A') && (day == 'S' && day2 == 'U') || ((num > 0) && (num <= 200)) && (duration > 0)){ call_cost = duration * billrate3; cout.precision(2); cout.setf(ios::fixed); cout << "The cost of the call : \$" << call_cost; } if (duration < 0) cout << "\nWrong input, you may try again!"; }while(duration < 0); } void EnterTime(char* hour, char* mins) { stringstream str, str2; cout << "\nEnter your time : "; cin.get(hour, 3); cin.get(mins, 3); str << hour; str2 << mins; int w, x; str >> w; str2 >> x; cout << "\nThe time entered is = " << w << ":" << x << endl; } void YesNo (char& select) { cout << "\n\nDo you want to continue? "; cin >> select; select = toupper(select); } int main() { char day, day2, select, hour[3], mins[3]; int duration, num; double call_cost; do { EnterDay(day, day2); TestCost(duration, num, call_cost, day, day2); EnterTime(hour, mins); YesNo(select); }while (select == 'Y'); return 0; } ``````
1) implementation of "EnterDay" is weird use std::string (or at least char *) and compare strings rather than char by char

2) 3 digit hour and 3 digit minutes?

3) Read http://www.cplusplus.com/reference/istream/istream/get/ once (more)
Topic archived. No new replies allowed.