In C++, create an input validation where the user enters a signed whole number and repeats this process until the user enters a zero. Do not accept any value with a decimal fraction or other extraneous characters, including dollar signs. The number's sign must be preserved. Some of the steps has been done for you. Fill out the program in C++. See below for the expected console output.
Program:
#include <iostream>
using namespace std;
int main(){
signed int input;
cout << "Enter a whole number: ";
cin>> input;
}
Expected Console Outputs:
Enter a number: $3444
Invalid input; try again.
Enter a number: $333
Invalid input; try again.
Enter a number: 344
Good input!
Enter a number: 344a
Invalid input; try again.
Enter a number: -344
Good input!
Enter a number: ddd
Invalid input; try again.
Enter a number: aaa
Invalid input; try again.
#include <iostream> using namespace std; int main() { signed int input; while (1) { cout << "Enter a number: "; if (cin >> input) { if (input == 0) break; cout << "Good input!" << endl; } else { cout << "Invalid input; try again." << endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } } return 0; }
Get Answers For Free
Most questions answered within 1 hours.