void stepOne(); |
int main() { stepOne(); } |
void stepOne() { std::cout << "This is step one" << std::endl; } |
int main() { stepOne(); stepTwo(); stepThree(); } |
void stepOne() { // get the user's score float score; std::cout << "Please enter your test score (0-100): "; std::cin >> score; } |
void stepOne() { // get the user's score float score; std::cout << "Please enter your test score (0-100): "; std::cin >> score; if (score < 49.5) { std::cout << "Sorry, but that is an F grade" << std::endl; } } |
void stepOne() { // get the user's score float score; std::cout << "Please enter your test score (0-100): "; std::cin >> score; // check for fail if (score < 49.5) { std::cout << "Sorry, but that is an F grade" << std::endl; } // check for A+ else if (score >= 89.5) { std::cout << "Congrats, that is an A+ grade" << std::endl; } } |
void stepOne() { // get the user's score float score; std::cout << "Please enter your test score (0-100): "; std::cin >> score; // check for fail if (score < 49.5) { std::cout << "Sorry, but that is an F grade" << std::endl; } // check for A+ else if (score >= 89.5) { std::cout << "Congrats, that is an A+ grade" << std::endl; } // give a message for anything in between else { std::cout << "That was somewhere in the D to A range" << std::endl; } } |
void stepTwo() { // get a positive number (greater than zero) from the user float posNum; std::cout << "Please enter a number greater than zero, e.g. 17.123: "; std::cin >> posNum; std::cout << "You entered " << posNum << std::endl; } |
void stepTwo() { // get a positive number (greater than zero) from the user float posNum; std::cout << "Please enter a number greater than zero, e.g. 17.123: "; std::cin >> posNum; // check for non-numeric input causing cin to fail if (std::cin.fail()) { std::cerr << "Sorry, only numeric input is accepted, please try again" << std::endl; std::cin >> posNum; } // echo their response back to them std::cout << "You entered " << posNum << std::endl; } |
void stepTwo() { // get a positive number (greater than zero) from the user float posNum; std::cout << "Please enter a number greater than zero, e.g. 17.123: "; std::cin >> posNum; // check for non-numeric input causing cin to fail if (std::cin.fail()) { std::cerr << "Sorry, only numeric input is accepted, please try again" << std::endl; std::cin.clear(); // clean up the internal error std::cin.ignore(80, '\n'); // clean out the garbage text in the input buffer std::cin >> posNum; } // echo their response back to them std::cout << "You entered " << posNum << std::endl; } |
void stepTwo() { // get a positive number (greater than zero) from the user float posNum; std::cout << "Please enter a number greater than zero, e.g. 17.123: "; std::cin >> posNum; // check for non-numeric input causing cin to fail if (std::cin.fail()) { std::cerr << "Sorry, only numeric input is accepted, please try again" << std::endl; std::cin.clear(); // clean up the internal error std::cin.ignore(80, '\n'); // clean out the garbage text in the input buffer std::cin >> posNum; } // otherwise, i.e. if cin didn't fail, we can check if the value was too small else if (posNum <= 0) { std::cerr << "Sorry, your value (" << posNum << ") was not greater than zero."; std::cerr << "Please try again" << std::endl; std::cin >> posNum; } // echo their response back to them std::cout << "You entered " << posNum << std::endl; } |
void stepThree() { // get an integer in the range 1 to 100 int score; std::cout << "Please enter an integer in the range 1-100: "; std::cin >> score; // check for non-integer input causing cin to fail if (std::cin.fail()) { std::cerr << "Sorry, only integer input is accepted, please try again" << std::endl; std::cin.clear(); // clean up the internal error std::cin.ignore(80, '\n'); // clean out the garbage text in the input buffer } // otherwise, i.e. if cin didn't fail, we can check if the value was too small else if (score < 1) { std::cerr << "Sorry, your value (" << score << ") was smaller than one."; std::cerr << "Please try again" << std::endl; } // otherwise we can check if it was too big else if (score > 100) { std::cerr << "Sorry, your value (" << score << ") was bigger than 100."; std::cerr << "Please try again" << std::endl; } // otherwise echo their response back to them else { std::cout << "You selected a valid value: " << score << std::endl; } } |
void stepThree() { // get an integer in the range 1 to 100 int score; std::cout << "Please enter an integer in the range 1-100: "; std::cin >> score; // check for non-integer input causing cin to fail if (std::cin.fail()) { std::cerr << "Sorry, only integer input is accepted, please try again" << std::endl; std::cin.clear(); // clean up the internal error std::cin.ignore(80, '\n'); // clean out the garbage text in the input buffer stepThree(); // start a new run of the function to try again } // otherwise, i.e. if cin didn't fail, we can check if the value was too small else if (score < 1) { std::cerr << "Sorry, your value (" << score << ") was smaller than one."; std::cerr << "Please try again" << std::endl; stepThree(); // start a new run of the function to try again } // otherwise we can check if it was too big else if (score > 100) { std::cerr << "Sorry, your value (" << score << ") was bigger than 100."; std::cerr << "Please try again" << std::endl; stepThree(); // start a new run of the function to try again } // otherwise echo their response back to them else { std::cout << "You selected a valid value: " << score << std::endl; } } |
int stepThree() { // get an integer in the range 1 to 100 int score; std::cout << "Please enter an integer in the range 1-100: "; std::cin >> score; // check for non-integer input causing cin to fail if (std::cin.fail()) { std::cerr << "Sorry, only integer input is accepted, please try again" << std::endl; std::cin.clear(); // clean up the internal error std::cin.ignore(80, '\n'); // clean out the garbage text in the input buffer score = stepThree(); // run stepThree again, record the value it gives back } // otherwise, i.e. if cin didn't fail, we can check if the value was too small else if (score < 1) { std::cerr << "Sorry, your value (" << score << ") was smaller than one."; std::cerr << "Please try again" << std::endl; score = stepThree(); // run stepThree again, record the value it gives back } // otherwise we can check if it was too big else if (score > 100) { std::cerr << "Sorry, your value (" << score << ") was bigger than 100."; std::cerr << "Please try again" << std::endl; score = stepThree(); // run stepThree again, record the value it gives back } // otherwise echo their response back to them else { std::cout << "You selected a valid value: " << score << std::endl; } // return the valid value we wound up with return score; } |
Reminder: the appropriate cycle to get/check is: - attempt to read the value into your int variable - if they entered a non-integer display a suitable error message (cerr) clear the error (cin.clear) clear the garbage input out of the buffer (cin.ignore) make a recursive call to try again, putting the returned value into your variable - otherwise if they entered a negative value display a suitable error message (cerr) make a recursive call to try again, putting the returned value into your variable - otherwise the value is fine - return the result
Please enter the number of times Shaggy answered a call: foo Sorry, the number of calls must be an integer value, please try again. Please enter the number of times Shaggy answered a call: -3 Sorry, the number of calls must be non-negative, please try again. Please enter the number of times Shaggy answered a call: huh? Sorry, the number of calls must be an integer value, please try again. Please enter the number of times Shaggy answered a call: 17 (at this point the program moves along normally)