Solved

read a variable # of ints from the keyboard into array

Posted on 1998-06-12
6
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

631 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