can someone give me simple code in c to check if the input enter by a user is an integer, float or a character
check if input is int float or char
Started by raonavneet, Jul 14 2008 09:34 PM
3 replies to this topic
#1
Posted 14 July 2008 - 09:34 PM
|
|
|
#2
Posted 14 July 2008 - 10:57 PM
Maybe there is something standard available in C for this. If not, you could try something like this:
--------------------------------------------------------------------
Ben Vaessen :: Home ::: Launch-IT :: Home
int idx;
char str[STRINGLENGTH];
int numbersfound = 0;
int dotsfound = 0;
for (idx=0; (idx<STRINGLENGTH) && (str[idx]); idx++){
numbersfound += ((str[idx] >= '0') && (str[idx] <= '9'));
dotsfound += (str[idx] == '.');
}
if (numbersfound == STRINGLENGTH)
printf("integer");
else if ((dotsfound == 1) && (numbersfound == STRINGLENGTH-1))
printf("floating point");
else
printf("string");
--------------------------------------------------------------------
Ben Vaessen :: Home ::: Launch-IT :: Home
#3
Posted 15 July 2008 - 04:59 AM
Yea, that code seems to be pretty good. I compiled it and it worked as asked.
#4
Posted 15 July 2008 - 09:49 AM
raonavneet said:
can someone give me simple code in c to check if the input enter by a user is an integer, float or a character


Sign In
Create Account

Back to top









