Need help with string conversion
I'm not getting the correct answers for my program. Can someone help figure out what's wrong? or show me some example of how you would do it?
It's not giving me the correct return for values that have a + or - preceeding the value, or it tells me the value is a float when it's not....
ie. -1 returns... -1 is not an integer
-1.2 is not a float
1..2 is a float
Any ideas?
Thanks!
If you want me to post the code let me know.
RE:Need help with string conversion
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char s[20];
int i;
float f;
int inc = 0;
int length = 0;
int isFloat = 0;
while ( strcmp (s , "EOF")!= 0){
printf("Enter a string or EOF to end: \n");
scanf("%s", &s);
printf("String: %s\n" , s);
i = 0; f = 0; isFloat = 0;
length = strlen(s); /* determine the length of the string */
for (inc = 0; inc < length; inc++){
if( (s[inc] < '0' || s[inc] > '9' && s[inc] == '+' || s[inc] == '-')){
i = 1;
if( s[inc] != '.' )
f = 1;
}
if( s[inc] == '.'){
isFloat = 1;
}
}
if ( i == 0 )
printf("This IS an Integer: %s\n", s);
else
printf("The string %s IS NOT an integer\n", s);
if (f == 0 && isFloat == 1){
printf("This is a Float: %s\n" , s);}
else
printf("The string %s IS NOT a floating point number\n", s);
if ( i == 1 && f == 1 )
printf("The string %s IS NOT a floating point number or integer\n", s);
}
return 0;
}
Need help with string conversion
Thanks Cela! That helped with the first problem regarding the positive and negative signs in front of the value...but any ideas on why the floating point number is returning that a string like
1..2 IS a floating point number?...it should be returning that it isn't a floating point number.
Thanks again!
re: Need help with string conversion
I also tried to use &&atof too, but it still gave me the same response....can you show me how I could use the counter for this?
Thanks again!