Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

proving if a string is an integer, floating point number or neither

Status
Not open for further replies.

1amnew2c

Technical User
Feb 10, 2003
3
US
Please help and resend your replies...my other account got deleted (it was handle iamnew2c) and I wasn't able to read the replies you sent to this question...How do I change my code so that I get 1..2 is not a float, or if there is a + or - sign in the string so that it gives me the number is neither?

Thanks again and I've provided the code again :>

C:\Borland\bcc55\Bin>0210a
Enter a string or EOF to end:
01234
String: 01234
This IS an Integer: 01234
The string 01234 IS NOT a floating point number
Enter a string or EOF to end:
1..2
String: 1..2
The string 1..2 IS NOT an integer
This is a Float: 1..2
Enter a string or EOF to end:
1.2
String: 1.2
The string 1.2 IS NOT an integer
This is a Float: 1.2
Enter a string or EOF to end:
eof
String: eof
The string eof IS NOT an integer
The string eof IS NOT a floating point number
The string eof IS NOT a floating point number or integer
Enter a string or EOF to end:
EOF
String: EOF
The string EOF IS NOT an integer
The string EOF IS NOT a floating point number
The string EOF IS NOT a floating point number or integer

C:\Borland\bcc55\Bin>bcc32 0210a.c
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
0210a.c:
Warning W8004 0210a.c 14: 'isFloat' is assigned a value that is never used in fu
nction main
Warning W8004 0210a.c 13: 'length' is assigned a value that is never used in fun
ction main
Warning W8004 0210a.c 12: 'inc' is assigned a value that is never used in functi
on main
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

C:\Borland\bcc55\Bin>

 
hi ...

there are two functions atoi and atof that can convert a string into integer and float respectively.

i don't understand your question completely. but i hope these two functions can be of some help to you ???
--
br: nagi
 
Code:
const char INT_VALID[] = "1234567890";
const char FLOAT_VALID[]= "1234567890.";

bool IsInteger(char* str)
{
   int len = strlen(str);

   return ( strspn(str,INT_VALID) == len);
}

bool IsFloat(char* str)
{
   int len = strlen(str);
   
   if(strspn(str,FLOAT_VALID) == len)
   {
      int count = 0;
      for(int i = 0;i<len;i++)
      {
          if(str[i] == '.')
               count++;
      }
      return count <=1;
    }
    return false;
}

and in your code

if(IsIngeger(str))
  cout<<&quot;the string &quot;<<str<<&quot; is an integer\n&quot;;
else if(IsFloat(str))
  cout<<&quot;The string &quot;<<str<<&quot; is a float\n&quot;;
else
  cout<<&quot;The string &quot;<<str<<&quot; is not an int or a float\n&quot;;

Matt

 
There are some flaws with this code: -12 , +45 etc wont be
accepted as integers because they had the signs plus and minus before them.And 3. , 5. etc will be accepted as float numbers.
So,this test is only valid for unsigned numbers excluding expression such as 1. , 5. , etc which are neither integers or float.

I had post some good examples but someone erased it, whatever...
 
What's the use of posting for students?
They just get deleted when they are discovered.
 
RE: jmnagi (Programmer): I tried using atof and atoi (as well as strtof and strtoi), but I must not understand the correct placement or sequence for these commands because I wasn't getting what I expected for an answer...I actually got errors and couldn't compile it....I also think I could add another variable and set up a counter to count how many non alpha numeric were in the string, but I couldn't figure out the correct code or placement of the code to get it to work either....

I have a set of strings that I need to test and the program needs to determine if they are a valid integer, floating point or neither....

RE:Zyrenthian (Programmer): Unfortunately, I haven't gotten to boolean logic yet ... Is it possible to give me some pointers/revisions with my code that I already have written?

RE:Leibnitz (Programmer):
Actually, it is working as written for integers...it's the floating point numbers that are the problem...it see's the first decimal and says that anything with a decimal is a float....

I appreciate your replies, but can you give some examples using my code that I've already written? :>

THANKS!
 
Matt,

I'll need to look at your code, but I think I can apply your counter logic to my program...I'll let you know what I get :>

if(strspn(str,FLOAT_VALID) == len)
{
int count = 0;
for(int i = 0;i<len;i++)
{
if(str == '.')
count++;
}
return count <=1;
}
return false;

Thanks again!
 
I post for this question because I remember in college they did not tell me about strspn. I ended up writing something like this using isalpha and isdigit. Also, in isdigit I had to test for the decimal. It got to be very tedious and I wish they explained these functions to me. If neg/pos numbers are entered with a sign, I agree that the functions will fail. There are some questions, in my opinion, that the professors should give some functions with to get you ready for the real world. It was not until I started working in the real world that I learned about the power of the strXXX functions. Same thing with math.h. We had to write our own pow functions etc. Maybe all teachers are not the same but all the ones I had were. I would have much rather learned how to apply existing functions like qsort then writing my own every time. Yes, I agree that learning how qsort works should be a tough process but once you are in your junior/senior year, you SHOULD be told about the functions at your disposal to save you time and prevent you from writing errors in recursion.

Just my 2cents :)

Matt
 
Can you post your code once again because without the code it is difficult to know exactly what is not working with your program.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top