reading file I/O

Reply

Join Date: Oct 2007
Posts: 33
Reputation: rogenie is an unknown quantity at this point 
Solved Threads: 0
rogenie rogenie is offline Offline
Light Poster

reading file I/O

 
0
  #1
Oct 20th, 2007
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?
  1. while (inputFile >> number)
  2. {
  3. //number of numbers on the file
  4. for (totalNumber = 0; totalNumber <= number; ++totalNumber)
  5. totalNumber++;
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: reading file I/O

 
0
  #2
Oct 20th, 2007
probably cos it's wrong.

Post all your code.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,148
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: reading file I/O

 
0
  #3
Oct 20th, 2007
you probably didn't initialize totalNumber when declaring it
  1. int totalNumber = 0;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 33
Reputation: rogenie is an unknown quantity at this point 
Solved Threads: 0
rogenie rogenie is offline Offline
Light Poster

Re: reading file I/O

 
0
  #4
Oct 20th, 2007
well here is the code. It outputs 516 instead of 200 random numbers I am trying to read off random.txt file.
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,148
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: reading file I/O

 
0
  #5
Oct 20th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 33
Reputation: rogenie is an unknown quantity at this point 
Solved Threads: 0
rogenie rogenie is offline Offline
Light Poster

Re: reading file I/O

 
0
  #6
Oct 20th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,148
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: reading file I/O

 
0
  #7
Oct 20th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 33
Reputation: rogenie is an unknown quantity at this point 
Solved Threads: 0
rogenie rogenie is offline Offline
Light Poster

Re: reading file I/O

 
1
  #8
Oct 20th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC