hello am new here.. can somebody tell me if there's a restriction for a decimal numbers entered? for instance,

cout<<"Enter number: "
cin>>num;

--- if the user put a decimal number on the variable num..
i want this to be INVALID INPUT in the terminal screen as a result.

any idea please.. it's c++.. thanks!

Recommended Answers

if (!(cin >> num))
    cerr << "INVALID INPUT\n";
Jump to Post

All 2 Replies

if (!(cin >> num))
    cerr << "INVALID INPUT\n";

hello am new here.. can somebody tell me if there's a restriction for a decimal numbers entered? for instance,

cout<<"Enter number: "
cin>>num;

--- if the user put a decimal number on the variable num..
i want this to be INVALID INPUT in the terminal screen as a result.

any idea please.. it's c++.. thanks!

before you can use narue's solution your 'num' variable has to of correct type. Because your checking if cin failed means that your num variable has to be of a type that does not support reading in floating numbers, for example if 'num' was of type char then entering in decimal number would fail it. But if it was like a std::string or char array or any floating or integral type then it wouldn't fail.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.