Get Int from file?

My current code is
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	//declare Vars and constants
	double input;
	double total = 0.0;

	ifstream myfile;
	myfile.open ("prices.txt");
	if (myfile.is_open())
	{
		//Enter input
		while(myfile.eof() == false)
		{
			myfile>>input;
			total = total + input;
		}//end while
		myfile.close();
	}//end if
	else
		cout << "Error opening file\n";
	system("pause");

	return 0;
} // end of main funct. 

I'm a bit rusty since we learned this, but it cant find the file.
I'm asking the obvious here, but is there a file called "prices.txt" in the same directory as the executable?
Fixed it, it was in the the folder, but in the first, not the deepest project folder. But now i have another problem.
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	//declare Vars and constants
	double input;
	double total = 0.0;

	ifstream myfile;
	myfile.open ("prices.txt");
	if (myfile.is_open())
	{
		//Enter input
		while(myfile.eof() == false)
		{
			myfile>>input;
			total = total + input;
		}//end while
		total = total/5;
		cout << "The Average is : " << total<< "\n";
		myfile.close();
	}//end if
	else
		cout << "Error opening file\n";
	system("pause");
	return 0;
} // end of main funct. 

When I run, the average is incorrect. The program gives me the average equal to 31.542
When I calculate the average myself on a calculator I get 28.074. The 5 numbers are
10.5
15.99
20
76.54
17.34
I already have been told that its suppose to be equal to 28.07 but I dont know why the program calculates incorrectly
Don't loop on eof(); loop on good() instead. The eof() isn't set until you've tried and failed to read from the file, so your loop will fail to read in the sixth time. This doesn't change the value of total, so the total gets an extra copy of the last 17.34, which, when divided by 5 gives you the incorrect result of 31.542.
Topic archived. No new replies allowed.