Thread: Getting Input to fill an array

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Getting Input to fill an array

    I don't quite understand how to get an input file to fill an array. It's part of a much bigger program, and I need this file (filled with 20 numbers) to be read in it's entirety (an eof?) to an array.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    So what have you tried? Do you know anything about arrays?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    I understand arrays pretty well. It's combining one with an input file that confuses me.
    Here is what I have so far:
    Code:
    void fill_array(int a[],int size, int& number_used) 
    
    {
         int next,index =0;
         using namespace std;
         ifstream in_stream;
         in_stream.open("SUPERNUMBERS.txt");
         if(in_stream.fail( ))
            {
                cout << "In stream failed.\n";
                exit (1);
            }
         in_stream.get(next);
         while (! in_stream.eof( ))
         {
               a[index] = next;
               index ++;
               cin >> next;
         }
         number_used = index;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fill the array help
    By IMMORTALX in forum C Programming
    Replies: 7
    Last Post: 09-09-2011, 03:34 AM
  2. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  3. fill array with number
    By nevrax in forum C Programming
    Replies: 9
    Last Post: 03-28-2007, 04:53 PM
  4. how to fill multiple array locations with one input
    By kryonik in forum C++ Programming
    Replies: 5
    Last Post: 06-08-2006, 10:12 PM
  5. Trying to fill an int array
    By boojus in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2003, 02:31 PM