isdigit? inputs numbers into array 'til user types an invalid integer

This is a discussion on isdigit? inputs numbers into array 'til user types an invalid integer within the C Programming forums, part of the General Programming Boards category; Hi, i'm new to C and i need a little help with a function i am writing to read in ...

  1. #1
    Unregistered
    Guest

    isdigit? inputs numbers into array 'til user types an invalid integer

    Hi, i'm new to C and i need a little help with a function i am writing to read in user input from the keyboard, a set of integers, and store them into an array. The input is terminated when the user types any invalid integer input. Integers are seperated by white space or new lines. Here is what I got so far... Do I use the ISDIGIT function as a condition for the loop? Also the (empty) array is created in the main program, and passed over to the function to add to it.

    --------------------------------------------------------------------
    #include <stdio.h>
    void get_input(char input_array[]);

    int main()
    {
    char input_array[999];
    get_input(input_array);
    return 0;
    }

    void get_input(char the_array[])
    {
    int i;
    for (i=0; ???????????; i++) {
    printf("Enter stream of integers: ");
    scanf("%c", &the_array[i]);
    }
    }
    --------------------------------------------------------------------

    I hope there aren't TOO many mistakes in there If anyone can give me any advice in how to create the condition for the loop, it would be much appreciated!

    Thankyou!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Avoid scanf when you can, it's evil.
    Code:
    void get_input ( char the_array[] ) 
    { 
      int i, ch;
      for ( i = 0; isdigit ( ch = getchar() ); i++ ) {
        if ( i >= ARRAYSIZE ) break;
        the_array[i] = (char)ch;
      }
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    Hi, thanks for the quick reply... I'm trying it right now...

    just a couple of questions... how come getchar() is preferred against scanf()? Is it because scanf is more basic, as that is what we are taught in class.

    Secondly, is ARRAYSIZE just any integer, or is it the maximum size of the array ie. in my program 999, or does it mean something using the sizeof() function?

    Thanks!

  4. #4
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    how come getchar() is preferred against scanf()? Is it because scanf is more basic
    Actually, scanf is a very complex function. Much more complex than getchar. That's one of the reasons why it should be avoided

    Secondly, is ARRAYSIZE just any integer, or is it the maximum size of the array ie. in my program 999, or does it mean something using the sizeof() function?
    It is the size of the array. It must be passed from main function. get_input() has no way to tell how large the array is.

    Code:
    void get_input ( char the_array[], int ARRAYSIZE )

  5. #5
    Unregistered
    Guest
    Great! Thanx for your help, Prelude & PutoAmo!

    Cheers!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how come getchar() is preferred against scanf()?
    Because scanf is evil and getchar is safer.

    >Is it because scanf is more basic, as that is what we are taught in class.
    You were taught that formatted input of any type with variable length arguments is more basic than reading one character at a time? I must have missed that class on C.

    >is ARRAYSIZE just any integer, or is it the maximum size of the array ie. in my program 999
    ARRAYSIZE is a macro that defines a named substitution for the value of 999, which in this case is the length of your array.
    #define ARRAYSIZE 999

    >It must be passed from main function.
    No it doesn't. The way I wrote it uses a macro which is a global value, if the function were to take a size variable then it would be safer to declare it in main and then pass it. But seeing as how the size of an array is a commonly used value in programs, defining a macro for it is considerably easier and simplifies the interface of your functions. As with all things of course, this should be used in moderation or you lose the advantages.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Unregistered
    Guest
    Hi, just read your reply, thanks for that Made things a lot clearer to me. From what you say, getchar does seem a lot easier... I don't know why my lecturer is teaching us scanf... maybe you can talk to him *lol* Anyways, thanks for your help!

    Cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM
  2. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 03:03 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. arrays
    By john_murphy69 in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 01:33 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21