944,065 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1297
  • C++ RSS
Oct 20th, 2007
0

reading file I/O

Expand Post »
why does this file outputs 516?? The value of numbers is being read from random.txt that has 200 random numbers in it. I was expecting this to output 200 and NOT 516.. whats up with this?
C++ Syntax (Toggle Plain Text)
  1. while (inputFile >> number)
  2. {
  3. //number of numbers on the file
  4. for (totalNumber = 0; totalNumber <= number; ++totalNumber)
  5. totalNumber++;
Reputation Points: 30
Solved Threads: 0
Light Poster
rogenie is offline Offline
33 posts
since Oct 2007
Oct 20th, 2007
0

Re: reading file I/O

probably cos it's wrong.

Post all your code.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 20th, 2007
0

Re: reading file I/O

you probably didn't initialize totalNumber when declaring it
C++ Syntax (Toggle Plain Text)
  1. int totalNumber = 0;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 20th, 2007
0

Re: reading file I/O

well here is the code. It outputs 516 instead of 200 random numbers I am trying to read off random.txt file.
C++ Syntax (Toggle Plain Text)
  1. //This will read the file random.txt,
  2. //then it will tell you the number of numbers in the file
  3. //it will sum it up
  4. //and give the average number
  5.  
  6. # include <iostream>
  7. # include <fstream>
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. ifstream inputFile;
  13. int number;
  14. int totalNumber = 0;
  15. int sumNumber = 0;
  16. double average;
  17.  
  18. inputFile.open("C:\\random.txt");
  19. if (!inputFile)
  20. cout << "Error opening file\n";
  21. else
  22. {
  23. while (inputFile >> number)
  24. {
  25. //number of numbers
  26. for (totalNumber = 0; totalNumber <= number; ++totalNumber)
  27. totalNumber++;
  28.  
  29. //sum of numbers
  30. sumNumber = sumNumber + number;
  31.  
  32. //average
  33. average = sumNumber/totalNumber;
  34. }
  35. inputFile.close();
  36. cout << "There are " << totalNumber << " in random.txt file" << endl;
  37. cout << "The sum of all these numbers is: " << sumNumber << endl;
  38. cout << "The average is: " << average << endl;
  39. }
  40. return 0;
  41. }
Last edited by Ancient Dragon; Oct 20th, 2007 at 6:36 pm. Reason: replace quote tags with code tags
Reputation Points: 30
Solved Threads: 0
Light Poster
rogenie is offline Offline
33 posts
since Oct 2007
Oct 20th, 2007
0

Re: reading file I/O

All you have to do inside the loop that starts on line 23 is (1) total up the value of all the numbers read from the file, which line 30 does ok, and (2) count how many numbers are read from the file, which line 27 does. Then after the file reading is finished you need to calculate the average, which line 33 does do, but its in the wrong place in your program (move it down below line 34 so that its outside the loop). That means line 26 is completly useless, and in fact destroys the value of totalNumber accumulator, so delete line 26.
Last edited by Ancient Dragon; Oct 20th, 2007 at 6:45 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 20th, 2007
0

Re: reading file I/O

Hi thanks. I tried putting the average calculation inside the while loop and it still worked. The line 26 is the one giving problem when I was trying to find out the total of numbers inside the file. Thanks.
Reputation Points: 30
Solved Threads: 0
Light Poster
rogenie is offline Offline
33 posts
since Oct 2007
Oct 20th, 2007
0

Re: reading file I/O

It is true that the average calculation will work inside the loop, but its a waste of processing time to leave it there. If a teacher wants to find the average grade of all students for a given test she/he does not recalculate the average every time a student's grade is added to the total. No because that's such a waste of her time. Instead she will wait until all student's grades are summed up then divide that total by the number of students.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 20th, 2007
1

Re: reading file I/O

Oh I see what you are saying. Every time the loop iterates, the average calculation will also calculate since it is a part of a loop. Instead having it outside the loop makes it look better and would be a good programming practice
Reputation Points: 30
Solved Threads: 0
Light Poster
rogenie is offline Offline
33 posts
since Oct 2007

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: RE:" THE BOUNCING BALL in C++"
Next Thread in C++ Forum Timeline: Data Table





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


Follow us on Twitter


© 2011 DaniWeb® LLC