eof error?

Mar 13, 2010 at 11:35pm
this program is inputting numbers from a file "input1.txt",stores them in an array then outputs them to screen,my problem is it outputs some additional strange numbers like -9.22596e+061 ,not sure how to fix this

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
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<cassert>

using namespace std;

const int size = 10;


void openFile(ifstream& infile);
void readScores(ifstream& infile,double scores[], int size);
void displayScores(const double scores[],int size);

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

openFile(infile);
readScores(infile,scores,size);
displayScores(scores,size);

	system("PAUSE");
	return 0;
}

void openFile(ifstream& infile)
{
	string infileName;

	
	cout << "Enter the name of the input file: " << endl;
	cin >> infileName;
	infile.open(infileName.c_str());
	assert(infile);	
}

void readScores(ifstream& infile, double scores[], int size)
{
	int i = 0;

	while(!infile.eof())
	{
		infile >> scores[i];
		i++;
	}
	size = i - 1;
	infile.close();
}

void displayScores(const double scores[], int size)
{
	cout << endl << "Contents of the list : " << endl;
	for (int i = 0; i < size; i++)
		cout << scores[i] << endl;
}
Mar 13, 2010 at 11:43pm
can you provide a sample input file?
I tried to reconstruct your mistake and created a file containing 10 numbers. Worked well.
Last edited on Mar 13, 2010 at 11:49pm
Mar 13, 2010 at 11:47pm
this is the exact input..
60 53 76 45 76 43 94
Mar 13, 2010 at 11:49pm
Ah, im sure you need to use a reference on size. (or pointer):

 
void readScores(ifstream& infile, double scores[], int &size)


Thats why your program do not know when to stop displaying numbers if you have less than 10.
You should do more error handling for mismatching input syntax and so on anyway.

But you want to change size although its a constant? sure? why do you make size a parameter if its a global variable? ohohoh...

Maikel
Mar 13, 2010 at 11:58pm
alrite problem fixed thanks!
Mar 14, 2010 at 7:42am
are you using the size variable right ?
Topic archived. No new replies allowed.