943,808 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 447
  • C++ RSS
Aug 24th, 2009
0

file operation

Expand Post »
Hi all,
I am having problem about file operations. There are a few numbers in my txt file. I want the program to calculate average of these numbers in the txt file. The code I have written is as below. Number of numbers in the file is 5. When I return count it returns 5 .The sum of numbers should be 92 but I get 94, and I get average 18.8 but the correct average should be 18,4 (txt file is attached). So can anyone help me about it? Do you think is it a good idea to calculate it in this way?
Thanks

the code:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. ifstream fin;
  4. fin.open("numbers.txt");
  5. if (fin.fail())
  6. {
  7. cout << "erorr on opening the file \n";
  8. exit (1);
  9. }
  10. cout << "the caculated average is " << avg_file(fin) << endl;
  11. double calculated_avg = avg_file(fin);
  12. }
  13. double avg_file(ifstream& source_file)
  14. {
  15. double number_in_file;
  16. double total = 0;
  17. int count = 0;
  18. source_file >> number_in_file;
  19. while (! source_file.eof())
  20. {
  21. source_file >> number_in_file;
  22. total = number_in_file + total;
  23. count ++;
  24. }
  25. //average of numbers in file
  26. return (count);
  27. }
Attached Files
File Type: txt numbers.txt (47 Bytes, 22 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
esesili is offline Offline
38 posts
since Apr 2009
Aug 24th, 2009
0

Re: file operation

double avg_file(ifstream& source_file)
{
	double number_in_file;
	double total = 0;
	int count = 0;
	while (source_file >> number_in_file)
	{		
		total = number_in_file + total;
		count ++;
	}	
	//average of numbers in file
	return (count);	
}
http://www.daniweb.com/forums/post15...tml#post155265
Last edited by Dave Sinkula; Aug 24th, 2009 at 5:57 pm.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 24th, 2009
0

Re: file operation

Two things:
First, you forgot to add the first number to the total.
Second, with while(!source_file.eof()) you run through the last line twice.

Therefore, instead of
10+20+40+10+12 = 92
you get
20+40+10+12+12 = 94
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008
Aug 24th, 2009
0

Re: file operation

Thanks for helps. Can you tell me how can I fix it?
Reputation Points: 10
Solved Threads: 0
Light Poster
esesili is offline Offline
38 posts
since Apr 2009
Aug 24th, 2009
0

Re: file operation

Click to Expand / Collapse  Quote originally posted by esesili ...
Thanks for helps. Can you tell me how can I fix it?
double avg_file(ifstream& source_file)
{
	double number_in_file;
	double total = 0;
	int count = 0;
	while (source_file >> number_in_file)
	{		
		total = number_in_file + total;
		count ++;
	}	
	//average of numbers in file
	return (count);	
}
http://www.daniweb.com/forums/post15...tml#post155265
Pay attention.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Output of struct to BIN file
Next Thread in C++ Forum Timeline: Why can a nested class access to the private member data?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC