In C++ How can I tell the difference between integers and characters?

Status
Not open for further replies.

Robertss

Newbie level 1
Joined
Nov 28, 2012
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,290
I am currently learning C++ and I have been asked to make a program which will calculate the interest that would be paid on a deposit of a given size. One of the requirements is that we display an error message when non-integer data is entered.

I however cannot work out how to detect if non-integer data has been entered. If anyone could provide an example of how this problem is solved it would be much appreciated!
 

By non-integer do you by any chance mean non-numeric ?

If so, all you need to do is check if your entered ascii character lies within 0x30 -0x39. These being the codes for key press 0..9
 

try this:
int a
if(!(cin>>a))
cout<<"error"
else if . . . .
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…