passing values into an array using a for loop from input file

Hi there, I'm trying to pass these numbers into the array using the for loop: 1, 2, 3, 4, 5. I created two files in my project called "inStuff.txt" and "OUTPUTS.txt". I know I have an error at my input in the for loop, can someone please point me to some example which is a bit similar to my problem or something? I'm also on linux using a crossGCC complier(not sure if that makes a difference).

Thanks


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
27
28
29
30
31
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	int scoresAr[5];
					
	int index;

	ifstream inFile;
	ofstream outFile;

	inFile.open("inStuff.txt");
	outFile.open("OUTPUTS.txt");

	for(index = 0; index < 5; index++)
	{
		cout << "enter number for " << index + 1 << ": ";
		cin  >> scoresAr[index](inFile);
		cout << endl << endl;
	}

	outFile << scoresAr[0] << endl << scoresAr[1] << endl << scoresAr[2]
	        << scoresAr[3] << endl << scoresAr[4];

	inFile.close();
	outFile.close();

	return 0;
}
Last edited on
On line 20 - what are you trying to do there? You probably want to change that whole for loop to be like this:
1
2
for (index = 0; index < 5; ++index)
    inFile >> scoresAr[index];


Of course, I'm not actually sure what you are trying to do here. If you give some more explanation on what you are trying to input from the file, that could be beneficial.
Oh my god you just explained everything, thank you. However why would we do ++index rather than index++?
Last edited on
No real reason in this case, its just force of habit for me (its the way I automatically do it). In theory, in some cases, it can be slightly more efficient to do a preincrement rather than a postincrement. Do which ever one you want.
yea, postincrement trips me up in while loops, but thanks for the help man
Topic archived. No new replies allowed.