Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

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!
 

rohitkhanna

Banned
Joined
May 1, 2012
Messages
341
Helped
76
Reputation
150
Reaction score
75
Trophy points
1,308
Location
New Delhi, India
Activity points
0
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
 

ahmad1954

Full Member level 4
Joined
Jun 30, 2012
Messages
192
Helped
26
Reputation
52
Reaction score
26
Trophy points
1,308
Activity points
2,102
try this:
int a
if(!(cin>>a))
cout<<"error"
else if . . . .
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top