Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-15-2008, 01:34 AM
raonavneet raonavneet is offline
Newbie
 
Join Date: Jul 2008
Posts: 1
Rep Power: 0
raonavneet is on a distinguished road
Red face check if input is int float or char

can someone give me simple code in c to check if the input enter by a user is an integer, float or a character
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-15-2008, 02:57 AM
bvaessen bvaessen is offline
Newbie
 
Join Date: Jul 2008
Location: Limburg
Posts: 27
Rep Power: 2
bvaessen is on a distinguished road
Default Re: check if input is int float or char

Maybe there is something standard available in C for this. If not, you could try something like this:

Code:
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-15-2008, 08:59 AM
Oigen Oigen is offline
Learning Programmer
 
Join Date: Jul 2008
Posts: 40
Rep Power: 2
Oigen is on a distinguished road
Default Re: check if input is int float or char

Yea, that code seems to be pretty good. I compiled it and it worked as asked.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-15-2008, 01:49 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: check if input is int float or char

Quote:
Originally Posted by raonavneet View Post
can someone give me simple code in c to check if the input enter by a user is an integer, float or a character
Maybe check the input string to first see whether it can be converted to an integer, then check to see whether it can be converted to a float, otherwise, assume neither?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help C++ program statquos C and C++ 3 06-12-2008 01:23 PM
C: how to check if the float is not a char Cheer C and C++ 11 04-09-2008 04:16 PM
I need help please! zizimetalique C and C++ 5 11-17-2007 10:39 PM
Please Help With A C Program!! siren C and C++ 7 04-17-2007 09:45 AM


All times are GMT -5. The time now is 02:22 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%

Ads