Solved

read a variable # of ints from the keyboard into array

Posted on 1998-06-12
6
165 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
Comment Utility
How about this....


0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:appleby
Comment Utility
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
Comment Utility
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
Comment Utility
Great minds think alike :-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now