Connecting Tech Pros Worldwide Forums | Help | Site Map

how to showing error message while integer is entered?

Newbie
 
Join Date: Jun 2007
Posts: 1
#1: Jun 17 '07
I'm new to C++ and am trying to write a code that will prompt the user to enter a character. my question is: how do i enter a condition that will printf an error message and prompt the user for a new entry?? i understand how it works, i just dont know what the condition is to recognize if a integer is enter. i also don't know what the condition is to recognize if a character is entered... my assignent is do today and i am stuck on that one part.. please help!

DeMan's Avatar
Lives Here
 
Join Date: Nov 2006
Location: Adelaide, SA
Posts: 1,748
#2: Jun 17 '07

re: how to showing error message while integer is entered?


Hi Josh K,

Welcome to thescripts. I'll move this post to the c/c++ forum where more experts will see it. If you are using printf and scanf, you can use the formaters %d, %c (for integer (d) and character (c)) I believe using cin and cout the problem is even simpler, but a c++ expert may correct me.

It's possible that %d will still accept a character and convert it to it's decimal equivalent, but if you read everything in as a character string, you can (quite trivially) creata a method to determine whether each character is a digit or letter....

Passing you over to c/c++ now....
Newbie
 
Join Date: Jun 2007
Posts: 15
#3: Jun 18 '07

re: how to showing error message while integer is entered?


Yep. Look for an ASCII table that lets you convert between letters/codes and then just use a bunch of if statements to see if it (the passed-in char) corresponds to any digit. I think there's something in the STL that does something similar, head on over to www.cplusplus.com and look at the reference for ctype.h, I THINK. Should be something like isalpha() (that can be used too, in a pinch) or isnumber() or something along those lines. Then printf away! (although as DeMan pointed out cout and cin are *much* easier to use)
Newbie
 
Join Date: Jan 2007
Posts: 14
#4: Jun 18 '07

re: how to showing error message while integer is entered?


Quote:

Originally Posted by Josh K

I'm new to C++ and am trying to write a code that will prompt the user to enter a character. my question is: how do i enter a condition that will printf an error message and prompt the user for a new entry?? i understand how it works, i just dont know what the condition is to recognize if a integer is enter. i also don't know what the condition is to recognize if a character is entered... my assignent is do today and i am stuck on that one part.. please help!

Hello josh K,
for ur problem u need to know ASCII values of alphabets and numbers..
once u will get them..then u ask for input. and chk its ASCII value if it comes in between 48 to 57 then u can print error msg and ask for input again..
48 to 57 is ASCII values for (0 to 9)..
- have a nice time.
Reply