DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   reading file I/O (http://www.daniweb.com/forums/thread93676.html)

rogenie Oct 20th, 2007 3:36 am
reading file I/O
 
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?
 while (inputFile >> number)
                {
                        //number of numbers on the  file
                        for (totalNumber = 0; totalNumber <= number; ++totalNumber)
                                totalNumber++;

iamthwee Oct 20th, 2007 5:53 am
Re: reading file I/O
 
probably cos it's wrong.

Post all your code.

Ancient Dragon Oct 20th, 2007 7:02 am
Re: reading file I/O
 
you probably didn't initialize totalNumber when declaring it
int totalNumber = 0;

rogenie Oct 20th, 2007 6:13 pm
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.
//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;
}

Ancient Dragon Oct 20th, 2007 6:43 pm
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.

rogenie Oct 20th, 2007 9:37 pm
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.

Ancient Dragon Oct 20th, 2007 9:58 pm
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.

rogenie Oct 20th, 2007 10:46 pm
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:)


All times are GMT -4. The time now is 7:31 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC