Thread: I need help with looping

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    49

    I need help with looping

    I wrote this to covert the string to double, but I'm having problem fixing the loop so that it goes through all the digits. when I run this it only converts the first which is -42.40000 and stops.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    main(){
           double result, convert(char s[]);
           char arr[]="-42.4 5 34 66";
           result=convert(arr);
           printf("%f", result);
           getchar();
           }
           
    double convert(char s[])
           {
           double val, power;
           int i, sign,pos,c;
           for (i =0; isspace(s[i]); i++) ;
           sign = (s[i] == '-') ? -1 : 1;
           if (s[i] == '+' || s[i] == '-')
           i++;
           for (val = 0.0; isdigit(s[i]); i++)
           val = 10.0 * val + (s[i] - '0');
           if (s[i] == '.')
           i++;
           for (power = 1.0; isdigit(s[i]); i++) 
              {
                val = 10.0 * val + (s[i] - '0');
                power *= 10;
              }
    return sign * val / power;
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, because you only call the "convert" function once, and convert itself converts only the first float in the string.

    You have two choices - either iterate over the strings in the main (skip to the next space each time you have converted a number), or build a list of double's in the convert functions. The lattter is probably best done by passing an array and array-size into the function and returning a count of how many you actually got converted.



    I would put braces around the "decimal" handling part, so that the for-loop to deal with the decimals is inside the if checking for a '.' in the string.

    --
    Mats

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    I'm a kinda finding figuring out how to implement what you said.

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by matsp View Post
    I would put braces around the "decimal" handling part, so that the for-loop to deal with the decimals is inside the if checking for a '.' in the string.
    This isn't necessary (like indentation isn't necessary hah, makes thing clearer tough). I'm sure you can easily figure why.

    But since i'm already writing things down, this is some kind of proof i'm posting for fun:

    Code:
    double convert(char s[])
           {
           double val, power;
           int i, sign,pos,c;
           for (i =0; isspace(s[i]); i++) ;
           sign = (s[i] == '-') ? -1 : 1;
           if (s[i] == '+' || s[i] == '-')
                  i++;
           for (val = 0.0; isdigit(s[i]); i++)
                  val = 10.0 * val + (s[i] - '0');
    
           /* A: s[i] isn't a digit */
    
           if (s[i] == '.')
           {
                  i++;
                  /* A: s[i] is something haha */
           }
    
           else
           {
                  /* A: s[i] isn't a digit, that means the condition in the for loop will be false on the first iteration */
           }
    
           for (power = 1.0; isdigit(s[i]); i++) 
              {
                val = 10.0 * val + (s[i] - '0');
                power *= 10;
              }
    return sign * val / power;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM