Thread: Help with input/output files

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Help with input/output files

    Hellow

    I'm new to this forum, and I like it alot
    It is just what I was looking for

    I'm new at c++
    for now, I'm at input/output files

    and I have a problem


    I have to write a program that reads some numbers (type double) from a file and outputs the avarage of it

    "Read numbers from the input file in a loop, incrementing count and updating sum with each number read."


    How do I have to read ALL files from the file
    can someone tell me how the loop has to look like


    here is my code so far


    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    
    int main()
    {
        ifstream fin;
        int count = 0;
        double val;
        double sum = 0.0;
    
        fin.open("numbers02.txt");
    
        //HERE HAS TO COME THE LOOP
    
        fin.close();
    
        if (count > 0) {
            cout << "Average of the " << count << " numbers in the file is " << 
                (sum/count) << "\n";
        }
        else {
            cout << "No numbers were found";
        }
    }



    I'm new with files ... that's why I have problems

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    OK, I have the loop now lol
    but it doesn't work like it should

    the file has 4 numbers
    but the output says that it is 5
    I know it is because count++ becomes 5
    but I don't know why that is

    and the avarage isn't correct because of that



    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    
    int main()
    {
        // Declare input stream
        ifstream fin;
        int count = 0;
        double val;
        double sum = 0.0;
    
        fin.open("numbers02.txt");
    	
        while (fin) 
        {
           fin  >> val;	// inputs values
           sum = sum + val;
           count++;
        }
    
        fin.close();
    
        if (count > 0) {
            cout << "Average of the " << count << " numbers in the file is " << 
                (sum/count) << "\n";
        }
        else {
            cout << "No numbers were found";
        }
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Code:
    while (fin>>val) 
        {
           sum = sum + val;
           count++;
        }
    I think this shouold work

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    This should help http://www.cplusplus.com/doc/tutorial/files.html lol nvm XD miss read some stuff O.o;

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    Quote Originally Posted by jlf029
    Code:
    while (fin>>val) 
        {
           sum = sum + val;
           count++;
        }
    I think this shouold work

    that worked
    thx

    can you explain why that is, because the other loop looks ok for me too
    I'm a newbie, I'm still learnig

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by aldin176
    that worked
    thx

    can you explain why that is, because the other loop looks ok for me too
    I'm a newbie, I'm still learnig
    The file's error status, which would cause the loop to fail the way you tried it in your previous example, does not get set until after an attempt to read from the file fails. When you read the last (the 4th) value from the file, the file is still "good" since the read attempt was a success and you increment the variable count to 4. Since the file stream is still in this "good" state, the loop continues one more time when it should not. The read fails and the stream state gets set to essentially "bad", count gets incremented to 5 (which is not what you wanted to have happen), and you will also increment sum by val which is likely to have the same value as it had during the last successfull read from the file. This other way demonstrated avoids this problem.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  2. Input/Output to external files, I NEED HELP!!
    By MrMax91423 in forum C++ Programming
    Replies: 3
    Last Post: 02-24-2006, 08:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM