Thread: Reading in integers into char Array

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Question Reading in integers into char Array

    I need to allow a user to input a stream of integers.

    The input should be terminated when the user types "end" OR ANY invalid integer input.
    The input should be read into an array of characters. the array needs to be converted into an integer array using atoi(but not restricted to).

    Would really appreciate some help, thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >the array needs to be converted into an integer array using atoi
    If all you want is an array full of one digit integers then it's easy:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
      int x = 0, c;
      char a[1024] = {'\0'};
      while ( isdigit ( c = getchar() ) )
        a[x++] = c;
      for ( x = 0; a[x] != '\0'; x++ )
        printf ( "%d ", a[x] - '0' );
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM