Thread: how to convert a numeric string to integer

  1. #1
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95

    how to convert a numeric string to integer

    The following code is supposed to convert a numeric string to an integer, but it does not work. I am trying to use GDB to see what is going on, but still can not follow the program. When I put in 1, the program return the right number but when I put in 12, the program returns -429. Here is the code:

    Code:
    #include <stdio.h>
    
    int main ()
    
    {
    
    int a;
    
    a = getint ();
    
    printf ("You entered ...%d", a);
    
    printf ("Press any key to continue ...");
    
    getch();
    
    }
    
    getint ()
    
    {
    
    int val,i,j,k;
    
    char str[6];
    
    i = 0;
    
    while ( i <= 5)
    
      {
    
        str[i] = getche();
    
        if ( str[i] == '\r')
    
          {
    
            str[i]='\0';
    
            break;
    
          }
    
        if ( str[i] == '\b')
    
          {
    
            printf ("\b");
    
            i--;
    
          }
    
        else 
    
          i++;
    
      } 
    
    val = 0;
    
    k = 1;
    
    for ( j = i-1; j >=0; j--)
    
      {
    
        val = val + ( str[j] - 48 * k);
    
        k = k * 10; 
    
      }
    
    return val;
    
    }
    I am also wondering what are the GDB outputs which start with $. I will try to do a more detailed search online; the GDB tutorials online do not seem to have anything about this. Below is a capture of what I mean:
    Code:
    (gdb) p k
    $3 = 1
    (gdb)
    $4 = 1
    (gdb)
    $5 = 1
    (gdb) s
    33      for ( j = i-1; j >=0; j--)
    (gdb) s
    35          val = val + ( str[j] - 48 * k);
    (gdb) p j
    $6 = 4
    (gdb) p val
    $7 = 6
    (gdb) s
    36          k = k * 10;
    (gdb) s
    33      for ( j = i-1; j >=0; j--)
    (gdb) s
    35          val = val + ( str[j] - 48 * k);
    (gdb) quit

  2. #2
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    I figured out my problem. I needed to add a ()s in line 38 to first subtract 48 from the value in the array and then multiply by k.

    Code:
          1 #include <stdio.h>
          2 int main ()
          3 {
          4 int a;
          5 a = getint ();
          6 printf ("You entered ...%d", a);
          7 printf ("Press any key to continue ...");
          8 getch();
          9 }
         10 getint ()
         11 {
         12 int val,i,j,k;
         13 char str[6];
         14 i = 0;
         15 while ( i <= 5)
          6 printf ("You entered ...%d", a);
          7 printf ("Press any key to continue ...");
          8 getch();
          9 }
         10 getint ()
         11 {
         12 int val,i,j,k;
         13 char str[6];
         14 i = 0;
         15 while ( i <= 5)
         16   {
         17     str[i] = getche();
         18     if ( str[i] == '\r')
         19       {
         20         str[i]='\0';
         21         break;
         22       }
         23     if ( str[i] == '\b')
         24       {
         25         printf ("\b");
         26         i--;
         27       }
         28     else
         29       {
         30         printf ("You entered ....%d", str[i]);
         31         i++;
         32       }
         33   }
         34 val = 0;
         35 k = 1;
         36 for ( j = i-1; j >=0; j--)
         37   {
         38     val = val + ( (str[j] - 48) * k);
         39     k = k * 10;
         40     printf ("J is %d", j);
         41     printf ("Here is what val is %d", val);
         42   }
         43 return val;
         44 }

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    In modern C we always explicitly declare the return value of a function. We also always use function prototypes where appropriate. You should do so for getint(). And you should probably explicitly return 0 at the end of main and declare getint and main as taking a void (although this is not technically necessary in C99 or higher).

    Since you've used non-portable functions getch and getche, and also the non-standard line ending character '\r' I can't actually run your code.

    It's not very handy to have an explicit "press enter to continue" wait for a keypress at the end of main. Why do you need to do that? If you're using an IDE that doesn't hold the console when the program ends, maybe there's a setting for that. You could also run the program from a console.

    Instead of 48, use '0'. And your math is wrong. Recall that the precedence of multiplication is higher than subtraction, so you need parentheses:
    Code:
            val += (str[j] - '0') * k;
    Normally it would be done like so:
    Code:
    val = 0;
    for (j = 0; j < i; j++)
        val = val * 10 + (str[j] - '0');

  4. #4
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Thank you so much. This helped a lot. This is not my code. I first try to do the problem, and then look up the answer. The book is an old turbo C book. I was lucky to find MinGw, which supports more dos functions than regular gcc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to convert string to integer
    By BIGDENIRO in forum C Programming
    Replies: 6
    Last Post: 11-21-2013, 09:04 PM
  2. to convert from string to integer
    By vopo in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 06:32 AM
  3. Is there a function to convert a numeric value into a string?
    By sundeeptuteja in forum C Programming
    Replies: 6
    Last Post: 11-06-2002, 02:11 PM
  4. how do i convert a string to an integer??
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-11-2001, 09:21 PM
  5. FAQ how do i convert a string to an integer?? (C)
    By Unregistered in forum FAQ Board
    Replies: 1
    Last Post: 12-02-2001, 05:03 PM

Tags for this Thread