reading integers from a file into an array. Help!

so my hw problem asks to write a C++ program to read integers from a file into an array. Assuming that the max number of integers that can be read from a file is 50. Read integers from the file into positions in the array starting with subscript 1 and continue reading integers until an end of the file conditions is encountered. The integers are found in the file "numbers.dat"

The program should display the entire list on screen
then display the numbers that appear in the "odd" position in the list (subscripts 1, 3, 5 etc)
then display the numbers that appear in the "even" position in the list
(subsctipts 2, 4, 6 etc)

The only thing I got was making the program display the entire list.

Here is what my program looks like

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void main()

{
ifstream infile;
ofstream outfile;
int num;

infile.open("numbers.dat");
if(infile.fail())
{
cerr << "Cannot open input file numbers.dat \n";
abort();
}
outfile.open("output.dat");

while (infile >> ws && !infile.eof())
{
infile >> num;
cout << setw(4) << num;
}

cout << endl;
cout << "Program finished\n";

system ("pause");
}

can anyone please help me.
I've tried asking the tutoring center at my school, friends but havent gotten much help.asking my professor is out of the picture...we only see him once a week and he doesn't go over hws...
Last edited on
As you read through the file, increment a variable indicating your position in an array and add values to the array as you read it.
Thank you for the reply but can you give/show me an example?
I'm really sorry, but I just started programming and I'm not that good with terminology.
I am more of a "see and learn type" if that makes sense
here is how you put number into an array with user input, now just apply this to your project and your done ;p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

const in SIZE = 10;

int main()
{
   int anArray[SIZE];
   
   cout << "\nEnter 10 numbers:\n";
   
   for (int i = 0; i < SIZE; i ++)
    { 
		cin >> anArray[i];

	}
	
	cout << "Here are the numbers in the array: "
	for (int i = 0; i < SIZE; i++)
	{
		cout << anArray[i];
	}
return 0;

}
Last edited on
Thanks for the reply but the problem I'm having has to do with is putting numbers into an array from a dat file not a user input.
I can't seem to put the numbers from the dat file into an array and give it a position at the same time
Topic archived. No new replies allowed.