void getBiggerThan(float &uVal, float min) { bool valOk = false; cout << "Please enter a number bigger than " << min << endl; while (!valOk) { cin >> uVal; if (cin.fail()) { cin.clear(); cin.ignore(80, '\n'); cerr << "That was not a number, please try again" << endl; } else if (uVal <= min) { cerr << "The value needs to be bigger than " << min << ", please try again" << endl; } else { valOk = true; } } } | The function works as intended (error checking and repeating until a valid value is given, then returning the value). |
void getBiggerThan(float &uVal, float min) { bool badVal = true; cout << "Please enter a number bigger than " << min << endl; do { cin >> uVal; if (cin.fail()) { cerr << "That was not a number, please try again" << endl; } else if (uVal <= min) { cerr << "The value needs to be bigger than " << min << ", please try again" << endl; } else { badVal = false; } } while (badVal); } | The program goes into an infinite loop if the user enters a non-number, but otherwise functions normally. [Explanation: because the cin.fail case is missing the cin.clear and cin.ignore] |
void getBiggerThan(float &uVal, float min) { bool isGood = false; int attempt; cout << "Please enter a number bigger than " << min << endl; cin >> uVal; for (attempt = 0; isGood != true; attempt++) { if (cin.fail()) { cin.clear(); cin.ignore(80, '\n'); cerr << "That was not a number, please try again" << endl; } else if (uVal <= min) { cerr << "The value needs to be bigger than " << min << ", please try again" << endl; } else { isGood = true; } } } | The program goes into an infinite loop on any incorrect user input. [Explanation: because the cin appears before the loop rather than inside it] |
void getBiggerThan(float uVal, float min) { cout << "Please enter a number bigger than " << min << endl; do { cin >> uVal; if (cin.fail()) { cin.clear(); cin.ignore(80, '\n'); cerr << "That was not a number, please try again" << endl; } else if (uVal <= min) { cerr << "The value needs to be bigger than " << min << ", please try again" << endl; } else { break; } } while (true); } | The input checking and repetition appear to run correctly, but the final correct value is never received by the calling function (e.g. main). [Explanation: because uVal is not pass-by-reference] |
void getBiggerThan(float &uVal, float min) { bool badVal = true; cout << "Please enter a number bigger than " << min << endl; while (badVal); { cin >> uVal; if (cin.fail()) { cin.clear(); cin.ignore(80,'\n'); cerr << "That was not a number, please try again" << endl; } else if (uVal <= min) { cerr << "The value needs to be bigger than " << min << ", please try again" << endl; } else { badVal = false; } } } | The program goes into an infinite loop because of a misplaced semi-colon. [Explanation:there should not be a semicolon in 'while (badval); {' ] |
void f(int x, int y) { int i = 1; while (i <= x) { cout << (i+y) << endl; i++; if ((i % 2) == 0) { y--; } } } | void f(int y, int x) { int j = 1; do { cout << (j + x) << endl; j++; if ((j % 2) == 0) { x--; } } while (j <= y); } |
void f(int i, int j) { int y = j; for (int x = i; x > j; x--) { cout << (x + y) << endl; y--; } } | void f(int x, int y) { int j = x; for (int i = y; i >= 1; i = i - 1) { int sum = i + j; j--; cout << sum << endl; } } |
void f(int x, int y) { for (int i = 0; i < y; i++) { cout << (x + y) << endl; } } | void f(int x, int y) { int i = 0; while (i < y) { int sum = x + y; cout << sum << endl; i++; } } |
void f(int x, int y) { while (x > 0) { if (y > x) { cout << x << endl; } x = x - 2; } } | void f(int x, int y) { do { if (x < y) { cout << x << endl; } x = x - 2; } while (x > 1); } |
void f(int x, int y) { int i = y; while (i <= x) { i = i + 2; y = y + 1; cout << (i + y - 1) << endl; } } | void f(int x, int y) { for (int j = y; x >= j; j = j + 2) { y++; cout << (y + j + 1) << endl; } } |
void f(int x, int y) { for (int r = 0; r < x; r++) { for (int c = 0; c < y; c++) { cout << "x"; } cout << endl; } } | xxxx xxxx xxxx |
void f(int x, int y) { int r = 0; while (r < y) { int c = 0; while (c < x) { cout << 'x'; c++; } cout << endl; r++; } } | xxx xxx xxx xxx |
void f(int x, int y) { int r = 1; do { int c = 1; do { cout << "x"; c++; } while (c <= y); r++; } while (r <= x); cout << endl; } | xxxxxxxxxxxx |
void f(int x, int y) { int r = 1; while (r < x) { int c = 1; do { cout << "x"; c++; } while (c < y); cout << endl; r++; } } | xxx xxx |
void f(int x, int y) { int r = 0; do { for (int c = 0; c <= y; c++) { cout << 'x'; } cout << endl; r++; } while (r <= x); } | xxxxx xxxxx xxxxx xxxxx |