Thread: input integers from standard input and use in a function

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    input integers from standard input and use in a function

    i'm trying to input a set of integers from the keyboard, and then put them into an array and use in a function; the integers can be any amount, say a maximum of 100. the problem is that, i'm not getting the input right and i don't know why. the code is like this:
    Code:
    int main(int argc, char *argv[])
    {
            int array[100];
            int k, n;
            scanf(" %d",&array);    
            int i;
            for (i=1; i<argc; i++){
             
                    if (array[i]=='\n')
                    {array[i] == '\0';
                    break;}
            printf("values: %d", array[i]);
            }
            n = sizeof(array);
       
            printf("%d\n", max(array, n));
            return 0;
    someone pls help me. i'm in urgent need

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    so i was able to get it to work for input from the keyboard, i still need to be able to redirect the input from a text file or smt and also for multiple sequences. the code so far is:
    Code:
    int main(int argc, char *argv[])
     {
            int alength, *a, k;
            alength = argc -1;
            a = (int *) calloc(alength, sizeof(int));
            for (k=1; k<argc; k++)
            {
            a[k-1] = atoi(argv[k]);
            }
    
    
         answer = max(a, alength);
    }

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    scanf()'s %d format reads a single int. If you want to read an array of ints, use a loop that calls scanf() repetitively.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    the problem i still have is this

    the first gives a segmentation fault

    while the second can only read from the keyboard.

    so how do i modify either one to read from both the keyboard and a redirected file

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post your failing code. If you are trying to scan into the particular element of an array, then you need to specify that spot:
    Code:
    scanf( "%d", &array[ x ] );

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Standard input---Help!
    By neogst in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2011, 12:33 AM
  2. Standard input way?
    By Milhas in forum C Programming
    Replies: 6
    Last Post: 03-27-2008, 03:58 PM
  3. end of standard input
    By alphaoide in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2004, 08:43 PM
  4. Getting standard input
    By winsonlee in forum C Programming
    Replies: 1
    Last Post: 04-04-2004, 08:51 PM
  5. Reading integers until end of input
    By nivo in forum C Programming
    Replies: 7
    Last Post: 10-20-2001, 04:18 PM

Tags for this Thread