Solved

read a variable # of ints from the keyboard into array

Posted on 1998-06-12
6
167 Views
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.
0
Comment
Question by:appleby
  • 2
  • 2
  • 2
6 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 1165770
How about this....


0
 
LVL 11

Expert Comment

by:alexo
ID: 1165771
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;
    }

OK?
0
 
LVL 32

Expert Comment

by:jhance
ID: 1165772
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");
            return;
      }

      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]);
      }
}
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:appleby
ID: 1165773
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?
0
 

Author Comment

by:appleby
ID: 1165774
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.

0
 
LVL 11

Accepted Solution

by:
alexo earned 70 total points
ID: 1165775
Great minds think alike :-)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question