Thread: Converting string to int

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    60

    Converting string to int

    Hey,

    I have written a piece of code to do as the title says, but I seem to have a problem in that any odd number of digits is converted wrong. eg. it converts 25 correctly but not 125.

    it gives me 1 less than the value I expect, output is 124 for input of "125"

    Here is my code, can anyone see any mistakes I've been looking over it for ages now and can't figure it out.
    Last edited by manutdfan; 01-06-2008 at 10:32 AM. Reason: no longer needed code

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Works as expected for me. 125 gives 125.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    Thats really odd, i've tried re-writing it into a single function which i've posted below and I still get the same problem.

    im using Dev C++ to compile in windows. Cheers for looking at it.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    int stringToInt(char s[])
    {
       int i = 0;
       int power = strlen(s) - 1;
       int value = 0, curNo = 0, sign = 0;
       /* test for negativity */
       if(s[i] == '-')
       {
          sign = 1;
          i++;
          power--;
       }
       while(s[i] != '\0')
       {
          curNo = s[i] - 48;
          if(curNo > 9 || curNo < 0)
          {
             printf("error");
             exit(1);
          }
          value = (value + (curNo * pow(10,power)));
          i++;
          power--;
       }
       if(sign == 1)
       {
         value = -(value);
       }
       return value;
    }
    int main(int argc, char* argv[])
    {
        printf("%d", stringToInt(argv[1]));
        return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your logic looks okay, but I suggest that there might be a floating point inaccuracy problem with your use of pow(10, power).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Indeed: (int)pow(10,2) prints as 99 on my system.

    Edit: That's not true, what I just said; what I meant was that curNo*pow(10,power) prints as 99. See:
    Code:
    int main(void)
    {
        int power=2, curNo=1;
        printf("&#37;d", (int)(1*pow(10.0,2)));
        printf(" =? %d", (int)(curNo*pow(10,power)));
        printf(" =? %d\n", (int)(10.0*10.0));
        return 0;
    }
    gives me 100 =? 99 =? 100 (using gcc/Dev-C++). I'm guessing the compiler can replace the first with the third, but not the second.

    So maybe replace your pow(10,power) with a variable that starts at 100000 (or wherever is appropriate) and gets divided by 10 each time.
    Last edited by tabstop; 01-06-2008 at 11:03 AM. Reason: accuracy

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Prints 100 for me. But that's really poor; there's no reliable pow function for integers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Prints 100 for me. But that's really poor; there's no reliable pow function for integers.
    Indeed. I see two options:
    1. Write your own pow-like function for ints.
    2. Reverse the portion of the numeric string consisting of digits, then iterate again, but with a multiplier that is itself multiplied by 10 on each iteration, starting with a multiplier value of 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    Cheers guys, that was the problem have just resorted to using a for loop to calculate pow value instead.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Why do you compare with the minus sign by writing '-' but when you subtract that value of '0' you write it explicitly as 48?

    If you're going to write '0' as 48 you might as well write '-' as 45, right?

    Just use a character literal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM