Won't compile, can't figure out why
I have this code I'm trying to convert a string to a float, I tried to compile it and got error message:
syntax error at end of input
I can't figure out what is causing this.
Also do you think this function will work properly for what I want (string to float conversion). If not why?
here is the function code and test driver:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
{ char test_string[81];
float status;
printf("\nEnter the string to convert: ");
scanf("%s",test_string);
printf("\nEchoing input string: %s",test_string);
status=string2Float(test_string);
printf("\nconverted float = %f",status);
printf("\nlenght of string=%d", strlen(test_string));
printf("\n");
system("pause");
}
float string2Float(char *s)
{ int neg=0, decplace=1, length=strlen(s), i=(length-1), p=0;
float result=0;
if(s[0]=='-')
{
p=p+1;
neg=1;
}
while(s[i]!='.')
{
decplace=decplace*10;
i=i-1;
}
for(p; p<length; p++)
{
if(s[p]=='.')
{
p=p+1;
{
result=result*10+(s[p]-48);
}
if(neg)
{
result=result*-1;
}
result=result*decplace;
return(result);
}
|
Thank you~Matt
You aren't matching all your braces
1 2 3 4 5 6 7 8 9
|
for(p; p<length; p++)
{
if(s[p]=='.')
{
p=p+1;
{ // You should reverse the brace like this: }
result=result*10+(s[p]-48);
}
|
Ah HA! I had a feeling it was a problem with the braces, just couldn't find it.
Thanks!!
Topic archived. No new replies allowed.