Thread: C++ File Question

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    30

    C++ File Question

    In this program I have to write a program that has to take the inputs of a file and output the average of the sum of numbers. My question is regarding how to average the numbers by adding the total numbers in the file. Would I have to do a count?

    Code:
    #include <fstream>#include <iostream>
    
    
    int main()
    {
        using namespace std;
        ifstream in_stream;
    
    
        cout << "I am going to take the numbers from the input file and average them." << endl;
    
    
        in_stream.open ( "input.dat" );
        if ( in_stream.fail( ) )
        {
            cout << "Please check if the file is saved properly. It could not open." << endl;
        }
    
    
        double first_number, next_number, total_numbers;
        int count = 0;
    
    
        in_stream >> first_number >> next_number >> total_numbers;
    
    
        first_number = 0;
        next_number = first_number + next_number;
    
    
        cout << "The average of the numbers is ";
    
    
        cout << ( first_number + next_number ) / total_numbers << endl;
        in_stream.close ();
    
    
        cout << "The program will now close." << endl;
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Not sure what you mean? Do you mean how to calculate the average? Then the answer is to add up all numbers and devide it by the number of numbers. F.ex.: The average og 4,5,6 = (4+5+6)/3.

    Other than that, it'd depend on how the file is formatted.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    You need a loop to keep reading numbers until there's nothing else to read. Since for the average you need the sum and the amount, in each iteration add the retrieved number to a sum buffer and increment a counter. When the loop ends, you know what to do.
    Just a sample:
    Code:
    double temp, sum = 0;
    int i = 0;
    
    while (inputFile >> temp)
    {
        sum += temp;
        ++i;
    }
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    Ok, That makes sense, I was once again making it harder than it should have been. When dealing with files do you suggest I only declare one variable rather than declaring many as I did before.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Many variables make a program needlessly hard to understand. Don't declare more than needed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about the file
    By thungmail in forum C Programming
    Replies: 14
    Last Post: 03-14-2008, 01:18 AM
  2. question about the file
    By thungmail in forum C Programming
    Replies: 1
    Last Post: 03-13-2008, 11:26 PM
  3. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  4. file io question
    By mike81 in forum C Programming
    Replies: 6
    Last Post: 05-16-2007, 07:05 PM
  5. File question-Testing if file exists
    By fuh in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2003, 07:11 PM