| | |
reading file I/O
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 33
Reputation:
Solved Threads: 0
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)
while (inputFile >> number) { //number of numbers on the file for (totalNumber = 0; totalNumber <= number; ++totalNumber) totalNumber++;
you probably didn't initialize totalNumber when declaring it
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Oct 2007
Posts: 33
Reputation:
Solved Threads: 0
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)
//This will read the file random.txt, //then it will tell you the number of numbers in the file //it will sum it up //and give the average number # include <iostream> # include <fstream> using namespace std; int main() { ifstream inputFile; int number; int totalNumber = 0; int sumNumber = 0; double average; inputFile.open("C:\\random.txt"); if (!inputFile) cout << "Error opening file\n"; else { while (inputFile >> number) { //number of numbers for (totalNumber = 0; totalNumber <= number; ++totalNumber) totalNumber++; //sum of numbers sumNumber = sumNumber + number; //average average = sumNumber/totalNumber; } inputFile.close(); cout << "There are " << totalNumber << " in random.txt file" << endl; cout << "The sum of all these numbers is: " << sumNumber << endl; cout << "The average is: " << average << endl; } return 0; }
Last edited by Ancient Dragon; Oct 20th, 2007 at 6:36 pm. Reason: replace quote tags with code tags
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.
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.
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- Reading a file into a Parallel Array (C++)
- problems with reading in file (C++)
- First year assigment on reading file, sorting and outputting invoice (C++)
- Error Message Concerning Reading File From A Drive (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- reading a file into code (Java)
- reading and printing a file to screen in C (C)
Other Threads in the C++ Forum
- Previous Thread: RE:" THE BOUNCING BALL in C++"
- Next Thread: Data Table
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






