ifstream problem

This program is supposed to have user input name of imputfile,open the file which should contain a list of scores and store the list in an array,then do some other stuff with those numbers,my probem is in the openFiles function..i keep getting an fstream error and cant figure out what im doing wrong for the life of me..heres the code
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
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>

using namespace std;

void openFile(ifstream& infile);
void readScores(double scores[],ifstream infile,int& arraySize);

int main()
{
ifstream infile;
int arraySize;
double scores[10];

openFile(infile);
readScores(scores,infile,arraySize);


	system("PAUSE");
	return 0;
}

void openFile(ifstream& infile)
{
	
	string infileName;
	cout << "Enter the name of the input file: ";
	cin >> infileName;
	infile.open(infileName.c_str());
}
void readScores(double scores[],ifstream infile, int arraySize)
{
int i;

while(infile)
{
	infile >> scores[i];
	i++;
}
}
What error are you getting? It may have something to do with the fact that you did not define arraySize, although I can't see it being used anywhere.
And readScores() has i be random garbage, so you a writing to a garbage offset of the scores pointer repeatedly.
Last edited on
well i just got rid of the readscores function and it compiled without any errors,so im happy.thanks
Whenever I have a user input a file name, I use string.data(). I've never tried it with .c_str().
.data() doesn't necessarily give you the null (which you typically need with c style strings)

better to use c_str()
Topic archived. No new replies allowed.