Can't locate input file, using arrays...

These are the instructions for the project. I can't deviate from this, the professor has a thing about us changing things.

"Collect an athlete's performance scores and calculate the final score using an array and input file.
Program Requirements:

* Allow the user to enter the name of the input file
* Use an assert statement or an if statement to check for a valid input file
* Store the scores in an array
* Use functions and incorporate value, reference and constant reference parameters appropriately."

This is all I can come up with:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>

using namespace std;

void openfile(ifstream& infilename);
void getscores(double scores[15], ifstream& infile);

//Call all functions and display data.
int main()
{

	ifstream infile;
	double scores[15];

	openfile(infile);
	getscores(scores, infile);


	system("pause");
	return 0;

}

//Locate and open the input file.
void openfile(ifstream& infile)
{
	string infilename;
	cout << "Enter the name of the input file: ";
	cin >> infilename;
	infile.open(infilename);
	assert(infile);

}

//Get scores from user, store in file
void getscores(double scores[15], ifstream& infile)
{
	int i;
	do
	{
		cout << "Enter a performance score: ";
		infile >> scores[i];
	}
	while (i <= 14);
}


It fails to locate the input file (TextFile1.txt), so I can't test the rest of the program since that first step is failing.
AND now it's suddenly giving me a bunch of other error messages and failing to start even though it at least ran the first step before, and I didn't touch the code.
Last edited on
You will have to use dynamic allocation (new/delete). There is a section of the tutorial on this site that covers this; if you have more questions after reading that, feel free to ask.
That's the only way? We haven't learned about dynamic allocation at all yet.
If the file could be any size, yes. Otherwise you can assign it some fixed size that is larger than anything you expect.
Okay, the professor clarified to make the size 15. Don't know why the instructions didn't just say that, but the array size is 15. I'm going to update the first post with my new code. It still isn't working- it won't locate the input file so I can't test anything else either. I just created a file with the default title C++ gives it, entered exactly that title, and the assertion failed.
Topic archived. No new replies allowed.