Why is this not working?

Hello guys!

For some reason, when I display the list of numbers I just get one random number instead of the real list. Also, I get negative numbers in the total and average.

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
int main(){
ifstream inFile;
	int num;
	inFile.open(filename, ios::in);
	(inFile >> num);
 int myarray[50];
	for (int x = 0; x < 50; x++)
	myarray[x] = 0;
	for (int x = 0; x < 50; x++)
	inFile >> myarray[x]; // Bringing a list of numbers from a file already created.

		int total = 0, count = 0;
		double average = 0.0;
		for (int x = 0; x < 50; x++)
			{
			total += myarray[x];

			count++;
				
			}
		inFile.close();
	average = static_cast<double>(total) / static_cast<double>(count);

				

		if (total == 0 && average == 0.0) {

		cout << "Data file is empty " << endl;
			}
			else
			{

			cout << endl << "Total :" << total << endl <<
	"Average :" << fixed << setprecision(2) << average << endl;
		cout << "File successfuly read" << endl;

		}


PD: I added all the headers that are required to get file accesibility. (I did not wrote them here in order to avoid a huge page of code)
Last edited on
How does the file look like? The first value you read goes to the variable num but it's not used for anything. Is that a mistake?
@Peter87 The file has 10 numbers on it, and for some reason, it only shows one digit that is not the first not last. Also, it's true. I never did anything with num.


@whitenite1 Yes. I did open the infile.
@srah

Here is your program, with minor fixes. If you have any questions about it, please ask.

I'm not really sure if your data file actually contained 50 numbers or not, but your program tries to read in 50 at the start of your program. I changed it to read in one number at a time, then increase count if it didn't fail. That way, we know how many actual numbers there are.

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
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <windows.h>

using std::ifstream;
using std::fixed;
using std::setprecision;
using std::string;
using std::cout;
using std::cin;
using std::endl;

int main()
{
	string filename = "NumList.dat";
	int num, x, count = 0;
	ifstream inFile(filename);
	int myarray[50];
	for (x = 0; x < 50; x++)
		myarray[x] = 0;
	x = 0;
	while (inFile >> myarray[x])
	{
		count++; // To find how many data entries
		x++;// increase x for next spot in myarray
	}
	cout << "Count = " << count << endl; // Display the count 
	inFile.close();
	cout << "File successfuly read" << endl;
	int total = 0;
	double average = 0.0;
	for (x = 0; x < count; x++) 
	{
		total += myarray[x];
	}
	average = static_cast<double>(total) / static_cast<double>(count);

	if (!total && !average)
	{
		cout << "Data file is empty " << endl;
	}
	else
	{
		cout << endl << "Total :" << total << endl;
		cout << "Average : " << fixed << setprecision(2) << average << endl;
	}
}
whitenite1, your code has a problem if there are more than 50 integers in the file.
@Peter87

Yeah. But I was basing the 50 on what the OP shows in their program. (S)He created an array of 50, so I figured that would be the limit of what was to be read.
Topic archived. No new replies allowed.