Thread: atio-What am I doing wrong?

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    21

    atio-What am I doing wrong?

    Part of my current assignment requires reading input from a file (Using I/O redirection) into a string of char, then parsing it, converting the numbers to int and saving that to an array of int. However, I can't get atoi to work for it-I'm using a wrong argument for it, but I don't know exactly how to fix it.

    Here's the segment in question:
    Code:
    i = atoi(input[cnt2]);
    I can provide the whole program if you need that as well. What's wrong with my argument?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Looks right to me. What is the result, and what is the content of input[cnt2]?

    I prefer to use strtol, as it gives a bit more control over what you do and detection cababilities for errors, but other than that, it shouldn't make any differences.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Acolyte View Post
    Here's the segment in question:
    Code:
    i = atoi(input[cnt2]);
    I can provide the whole program if you need that as well. What's wrong with my argument?
    Might you mean either
    Code:
    i = atoi(input);
    or
    Code:
    i = atoi(&input[cnt2]);
    ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    21
    Changing it to
    Code:
    i = atoi(&input[cnt2]);
    removed the error, but it's still not working. Here's the whole section:

    Code:
    for (cnt2 = 0; cnt2 < 30; cnt2++)
                            {
                                    if (input[cnt2] != ' ')
                                    {
                                            i = atoi(&input[cnt2]);
                                            stack[cnt3] = i;
                                            cnt3++;
                                    }
                            }
    Basically, what it does is counts through (char input[30] and is supposed to, when it finds anything that is not a space (The only inputs will be numbers and spaces), it converts it to int and puts it into (int stack[cnt3].

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >when it finds anything that is not a space
    That strikes me as suspicious. Will all of the numbers only be one digit? If not, are you expecting something like 123 to be broken down into 123, 23, and 3? I'm not sure this is the effect you want:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      const char *p = "123 456 789";
    
      while ( *p != '\0' ) {
        if ( *p != ' ' )
          printf ( "%d\n", atoi ( p ) );
        ++p;
      }
    
      return 0;
    }
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    21
    Damn-that's something I hadn't considered. How would I go about dealing with multiple-digit numbers, then?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How would I go about dealing with multiple-digit numbers, then?
    Use strtol instead. It'll tell you where the conversion stopped, and you can use that to continue. Even better, strtol (and atoi) skip leading whitespace, so it's as simple as this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      const char *p = "123 456 789";
      char *end = p;
    
      while ( end != NULL && *end != '\0' )
        printf ( "%d\n", strtol ( end, &end, 0 ) );
    
      return 0;
    }
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    21
    EDIT: NVM, found the problem.
    Last edited by Acolyte; 10-05-2007 at 05:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM