void partOne(); |
int main() { partOne(); } |
void partOne() { std::cout << "This is part one" << std::endl; } |
int main() { partOne(); partTwo(); partThree(); } |
void partOne() { // get the user's score float score; std::cout << "Please enter your test score (0-100): "; std::cin >> score; } |
void partOne() { // 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 partOne() { // 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 partOne() { // 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 partTwo() { // 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 partTwo() { // 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 partTwo() { // 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 partTwo() { // 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 partThree() { // 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 partThree() { // 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 partThree(); // 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; partThree(); // 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; partThree(); // 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 partThree() { // 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 = partThree(); // run partThree 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 = partThree(); // run partThree 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 = partThree(); // run partThree 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
Within building 315, how many times did you travel from floor 1 up to floor 2? ten Sorry, that was not an integer, please try again Within building 315, how many times did you travel from floor 1 up to floor 2? -10 Sorry, it cannot be a negative number of times, please try again Within building 315, how many times did you travel from floor 1 up to floor 2? -1 Sorry, it cannot be a negative number of times, please try again Within building 315, how many times did you travel from floor 1 up to floor 2? oops Sorry, that was not an integer, please try again Within building 315, how many times did you travel from floor 1 up to floor 2? rats Sorry, that was not an integer, please try again Within building 315, how many times did you travel from floor 1 up to floor 2? 17 ...here the program could finally go ahead and use the value 17 they provided