-1

I just begin to learn C and here is my program whose task is to get a value of float number inside a string. The program will stop when meet character '$'. It find where the floating point is located and then using its location as reference to get the value. There's no problem in compiling but when running, , it return 0.000 for any input value

I can't find what's wrong in this program so

here is my code

#include "stdio.h"
float power( int x, int y);
int main()
{
    int i=0, e=0; 
    float x=0;
    char str[100];
    scanf("%s", str[0]);
    for (i=0; i<100; i++){
        if (str[i]='.'){
            for (e=0; e<i; e++){
                x += str[e]*power((i-e-1),10);
            }
        }
        break;
    }
    i++;
    while (str[i]!='$'){
        x += str[i]*power((e-i),10); // str[e] == '.'
        i++;
    }
    printf("%f", x);
    return 0;
}

float power( int x, int y) //simple power for int base
{
    int i, temp;
    temp=y
    if (x>=0){
    for(i=0; i<x; i++){
    }
        y=y*temp;
    }
    else{
            for(i=0; i>x; i--){
                y=1/temp;
            }
    }
    return y; 
}
aukxn
  • 231
  • 1
  • 4
  • 8
  • `scanf` can return a floating point number directly... – Jason Hu Mar 12 '15 at 18:42
  • @HuStmpHrrr you mean, you can use `scanf()` to retrieve the floating point number directly, because it doesn't return a floating point number. – Iharob Al Asimi Mar 12 '15 at 18:44
  • 2
    Your `scanf()` is anyway wrong `scanf("%s", str[0])`? maybe `scanf("%s", &str[0])`? which is the same as `scanf("%s", str)`, and to be safe it's better to `scanf("%99s", str)` and also check the return value.. – Iharob Al Asimi Mar 12 '15 at 18:45
  • 1
    Well for one, your negative power handling is broken -- the `y=1/y` for loop will alternate `y, 1/y, 1/(1/y)==y,1/(1/(1/y))==1/y, ...`. Instead you should put the `x<0` check at the beginning of the function with `y=1/y;x=-x` once, and then let the positive power handle it. Oh, and your positive power calculation will produce y^(2^x) not y^x; you should use something like http://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int – zebediah49 Mar 12 '15 at 18:47
  • 1) `scanf("%s", str[0]);` 2) `i<100` 3) `if (str[i]='.'){` 4) `break;` ..... – BLUEPIXY Mar 12 '15 at 18:49
  • I just think that >scanf("%s", str[0]) will tell compiler where the value will be storage in the array in case of I don't want it to be 1st element of array. Is there anyway to do it? – aukxn Mar 12 '15 at 18:50
  • 1
    What's with the 50 line poem shaped thing instead of a simple half a liner with `scanf`? – Blindy Mar 12 '15 at 18:55

1 Answers1

0

The problem is here

scanf("%s", str[0]);

maybe you mean

scanf("%s", &str[0]);

or even better

scanf("%99s", str);

and do not ignore the return value, if it happens to be EOF then what happens next will be undefined.

To convert the string to a floating point number use strtof(), this way

char *endptr;
float value;

value = strtof(str, &endptr);
if ((*endptr != '\n') && (*endptr != '\0'))
    printf("Cannot convert `%s' to float\n", str);
else
    printf("%f\n", value);

but you could also and it's the preferred approach

float value;

if (scanf("%f", &value) != 1)
    printf("Cannot convert `%s' to float\n", str);
else
    printf("%f\n", value);
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97