Array output incorrect

Hi,

I'm having trouble figuring out this error.I'm reading an array from a file and displaying to the screen. My .dat file is as follows:
20
12
40
30
30
15

Code is as follows:

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
// This program reads employee information from a file and stores them
// in an int array. It uses one for loop to input the hours and another
// for loop to display them.
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
	const int NUM_EMPLOYEES = 6;    // Sets number of employees
	int hours[NUM_EMPLOYEES];       // Holds each employees hours
	int count;                      // Loop counter
	ifstream datafile;              // used to read data from file

	// Open data file
	datafile.open("work.dat");
	if (!datafile)
		cout << "Error opening the data file\n";
	else
	{  // Input hours worked by each employee
		for (count = 0; count < NUM_EMPLOYEES; count++)
			datafile >> hours[count];
		datafile.close();

		// Display the contents of the array
		cout << "The hours worked by each employee are\n";
		for (count = 0; count < NUM_EMPLOYEES; count++)
		{ cout << "Employee " << count+1 << ": ";
		cout << hours[count] << endl;
		}
	}
	return 0;
}



Here is my output:


The hours worked by each employee a
Employee 1: -858993460
Employee 2: -858993460
Employee 3: -858993460
Employee 4: -858993460
Employee 5: -858993460
Employee 6: -858993460
Press any key to continue . . .


What am I doing wrong?

Thanks in advance.
Hi I couldn't see any error in your code so i copied it into my IDE and compiled it and it worked fine. What i think might be the problem is the formatting in your work.dat file. What program are you using to create it?
I used OpenOffice to create the file.
And what format did you use...?
It needs to be plain text.
I saved the file as work.dat. I thought it had worked for me in the past, but I could be mistaken.

I tried re-creating the file as work.txt and making the apprpriate changes in my code, but I still come up with the same results.
You're not supposed to give it a different name, you're supposed to save it in plain text format.
Just use a different editor that only supports plain text.
try using notepad if you're on windows that worked for me
That worked like a charm! Thank you so much.

I used Wordpad to create the .txt file and it worked fine. Thanks again.
Last edited on
Topic archived. No new replies allowed.