We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


read a variable # of ints from the keyboard into array

appleby asked
Medium Priority
Last Modified: 2011-04-14
I'm looking for a sample program that does exactly this:  gets called from the command line to read in a variable number of integers which get stored into an array.  After whatever manipulation (sorting, adding, multiplying various parts of the array) the array will be displayed back on the screen and the program ends.  Usage should be:
  FeedMe 383 22 82 99 1 93 3 82
for any amount of ints up to 100.  I'd like sample code for FeedMe.cc and FeedMe.h, as well as a sample compile on gcc.
This should be really simple and hopefully someone has it lying around and can post it asap.  Thanks.
Watch Question

How about this....

How about:

    #include <iostream.h>
    #include <stdlib.h>

    int Sum(int size, int* array)
        int result = 0;
        for (int i = 0; i < size; ++i)
            result += array[i];

        return result;

    int main(int argc, char* argv[])
        if (argc < 2)
            cout << "Usage: " << argv[0] << " <argument>..." << endl;
            return 1;

        int i;

        int size = argc - 1;
        int* array = new int[size];
        for (i = 0; i < size; ++i)
            array[i] = atoi(argv[i + 1]);

        int result = Sum(size, array);

        cout << "Array: ";
        for (i = 0; i < size; ++i)
            cout << array[i];
        cout << endl;

        cout << "Result: " << result << endl;

        delete[] array;
        return 0;


Oops, here's the code:

#include <stdio.h>
#include <stdlib.h>

void main(int argc, char *argv[])
      // Make a place to store the ints
      if(argc < 2){
            fprintf(stderr, "Error, nothing specified\n");

      int *Ints = new int[argc-1];
      for(int i=1; i<argc; i++){
            Ints[i-1] = atoi(argv[i]);
            printf("Argument #%d (%s) converted to %d\n", i-1, argv[i], Ints[i-1]);


I was going to comment that I get segmentation fault, but since I see that both suggestions are the same, it makes me think I have a typo or something.  But I don't see one.  Have you both tested these?


Ok, I was just running an old file before fixing a typo.  Both are fine.  Thanks very much.  Alexo's answer did come through first (even if by mistake) and was more complete with the function example also.  I feel I should give him the grade.

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.