Question: I have to write a c++ program were it would ask for you to input a numerical value and the numerical value will be displayed

I have to write a c++ program were it would ask for you to input a numerical value and the numerical value will be displayed in the output. However the program should only allow numerical values so symbols, letters, and blank spaces should not be allowed. I am having problem when I input a value of 134abc the value inputed should not be allowed but it is, and also Im having trouble when I enter a blank value "just enter" it just ignores this input rather than displaying an error in the input. Also when you input the number 0 the program does not shutdown. REally need Help Thank You!

This is my code right now:

#include #include

using namespace std;

/* * */ int main() { cout << "Enter an int or (0) to exit: "; char space = ' '; int x; if (cin == space){ cout << "oops"; }

if (cin == 0){ cout << "Bye!!"; exit(0); } while(!(cin >> x)){ cin.clear(); cin.ignore(numeric_limits::max(), ' '); cout << "Invalid input. Try again: "; } cout <<"Display number:" << x; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!