i trying write program can calculate future day inputting today , number of days elapsed. part, works. however, stumbled upon tiny problem , have been stuck past hour.
do { cout << "to begin, enter today's day: " << endl << "0. sunday" << endl << "1. monday" << endl << "2. tuesday" << endl << "3. wednesday" << endl << "4. thursday" << endl << "5. friday" << endl << "6. saturday" << endl; cin >> num1; while (num1 < 0 || num1 > 6) { cout << "the number must in range 0 6.\n"; cout << "please try again: "; cin >> num1; } cout << "enter number of days elapsed after today: "; cin >> numdays; if (num1 == 0) { today = "sunday"; } ... /* similar cases 1 - 5 */ if (num1 == 6) { today = "saturday"; } n = numdays % 7; switch (n) { case 0: cout << "today " << today << " , " << num1 << " days sunday" << endl; break; ... /* similar cases 1 - 5 */ case 6: cout << "today " << today << " , " << num1 << " days saturday" << endl; break; default: cout << "please enter valid response" << endl; } cout << "press r try again" << endl; //prompts user try again cin >> response; system("cls"); } while (response == 'r' || response == 'r');
here's part of it. can see, program supposed ask user if wants try again. works default in switch, closes right away instead of asking me if want try again. having sort of misconception or what?
if while giving input numdays
, trailing character present, may read in response
. can confirm printing ascii value of response
.
to overcome issue, following before reading in response:
cin.ignore(256,'\n'); /* ignore lingering characters */ cout << "press r try again" << endl; cin >> response;
further reading: why call cin.clear() , cin.ignore() after reading input?