C++ using fstream to calculate average and standard deviation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: C++ using fstream to calculate average and standard deviation

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Posts
    9

    Post C++ using fstream to calculate average and standard deviation

    Hey, My first time using the forums.
    I have comments in my code to kind of explain what's going on but basically my main problem is it is not letting my close a file and then reopen it.(I FIGURED THIS OUT)
    so now it works very well (I had to make a few little adjustments because I messed up some of the input things and little "off by one" errors)

    My only problem left is that it does not write to the file that a calculation error occurred.
    This is a minor thing but if you have any ideas I would appreciate them.



    Thanks for any help.







    Code:
    
    //
    //p365 num. 4
    //
    //calculate average and  standard deviation of a specified file and repeat as needed
    //
    //
    //possible logic error location are marked by the phrase ERROR HERE
    //it doesnt tell file that an error occured
    //
    //
    
    
    #include <iostream>
    #include <fstream>
    #include <cstdlib> //for key word - exit
    #include <cmath> // for keyword - sqrt
    
    using namespace std;
    
    void avgcalc(ifstream& receive, double& average);
    //ifstream has been opened, ready to take double input
    //output the average using call be reference
    
    void stddevcalc(ifstream& receive, double average, double& standarddeviation);
    //ifstream has been opened, ready to take input. Average has been calculated
    //output standard deviation using call by reference
    
    int main ()
    {
        ifstream receive;
        ofstream deliver;
        char yorn; //for yes or no answer to repeat calculations
        int abcd=0;
        
        cout << "AVERAGE AND STANDARD DEVIATION\n\n"; //title
        
        
        do
        {
             double standarddev=0, avg=0, abc=0; //declare the variables to be changed by call by reference, and also abc. abc is used so that the first if statement is only run after the first time it fails
             char fileget[22], filegive[22]="RESULTS.txt", pause; // set variable for file to be calculated and file to be sent to. I use a dummy char variable because I am running in an IDE and it keeps the output on the screen 
             
             do
             {
                  if(abc > 0) //so error message is output to screen after the first file is not found or there is an error
                  {
                       cout << "receive/fileget/first open " << fileget << " - FAILED to OPEN\n";
                  }
                  cout << "File name: "; //so the user can specify which file to use for calculations
                  cin >> fileget;
                  receive.clear();
                  receive.open(fileget);
                  
                  abc++; //as stated, this is to control error output
             }while(receive.fail()); //allows user to re-input the file name until he gets it right 
    
             cout << "receive ifstream has been opened for first calculation\n";
    
             if(abcd<=0)
             {
                  deliver.open(filegive, ios::app); //it is set so that it can be appended so that multiple calculations can be done and the user history is kept
                  if (deliver.fail())
                  {
                       cout << "deliver/filegive/first open - FAILED to OPEN\n";
                       cin >> pause;
                       exit(1);
                  }
             }
             abcd++;
             cout << "deliver ofstream has been opened\n";
             
             avgcalc(receive, avg);     //average calculation here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
             
             deliver << "--- For File " << fileget << " ---\nAVG: " << avg << endl; //output the average results to the file
             
             receive.close();
             
             cout << "receive ifstream has been closed\n";
             receive.clear();
             receive.open(fileget); //ifstream is closed and re-opened so that the numbers can be read through again by the next function
             if(receive.fail())
             {
                  cout << "receive/fileget/second open " << fileget << " - FAILED to OPEN\n";
                  deliver << "Standard Deviation Calculation ERROR\n\n"; //this will output to the file to show that an error occured ERROR HERE
                  cin >> pause;
                  exit(1);
             }
             
             cout << "receive ifstream has been opened for second calculation\n";
             
             stddevcalc(receive, avg, standarddev);        //standard deviation calculation here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
             
             deliver << "STANDARD DEV: " << standarddev << endl << endl; //standard deviation is added to the file here
             
             receive.close();
             
             cout << "receive ifstream has been closed\n";
             
             cout << "Calculations Complete and Sent to " << filegive << "\n\nAgain?\n"; //prompt for another loop
             cin >> yorn;
         }while((yorn != 'n')&&(yorn != 'N'));
    
     return 0;   
    }
    
    
    //~~~~~~~\~~~~~~~~~~\~~~~~~~~~\~~~~~~~~~~~~\~~~~~~~~~\~~~~~~~~~~\~~~~~~~~~~~\~~~~~~~~~~~\~~~~~~~~~~\~~~~~~~~~~~~\~~~~~~~~~~~~~~~~~\~~~~~~~~~~~~~~\~~~~~~~~\~~~~~~~~\~~~~~~~~~
    
    
    void avgcalc(ifstream& receive, double& average)
    {
         double number, total=0, count=0;
         
         while(receive >> number) // to keep the loop going while there is data left in the file and get next number in file
         {
              total+=number; // add number to total
              count++; //increment count
         }
         
         average = (total / count); //calculate average
    }
    
    void stddevcalc(ifstream& receive, double average, double& standarddeviation)
    {
         double number, add=0, count=0, sqr=0, sub=0;
         
         while(receive >> number)
         {
              sub = number - average; //first step of calculating standard deviation
              sqr = sub*sub; //second step of calculating standard deviation. square the first step
              add+=sqr; // third step. add the squared terms up
              count++; 
         }
         
         standarddeviation = sqrt(add/count); //find the average of the squared terms and square root it --> standard deviation
    }
    Last edited by wsad597; March 8th, 2011 at 06:43 PM. Reason: the formating was improper. This should be better. and the second edit was to refine the question

  2. #2
    Join Date
    Mar 2011
    Posts
    3

    Re: C++ using fstream to calculate average and standard deviation

    nice post, thanks for sharing this usefull information
    _______________________
    Rakeback

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: C++ using fstream to calculate average and standard deviation

    I'm sorry about the formatting if it doesn't work on your screen.
    That's why we have code tags.

  4. #4
    Join Date
    Mar 2011
    Posts
    9

    Re: C++ using fstream to calculate average and standard deviation

    Quote Originally Posted by Skizmo View Post
    That's why we have code tags.
    ahhhhhh. Thank you.

  5. #5
    Join Date
    Mar 2011
    Posts
    9

    Re: C++ using fstream to calculate average and standard deviation

    Here it is with a code tag.




    Code:
    //calculate average and standard deviation of a specified file and repeat as needed
    //
    //
    //possible logic error location are marked by the phrase ERROR HERE
    //fails to open file the second tiime every time. Never been able to calculate standard deviation. Also it doesnt tell file that an error occurred
    //also, once you input the wrong file name once it says all other file names are incorrect too
    //
    
    
    #include <iostream>
    #include <fstream>
    #include <cstdlib> //for key word - exit
    #include <cmath> // for keyword - sqrt
    
    using namespace std;
    
    void avgcalc(ifstream& receive, double& average);
    //ifstream has been opened, ready to take double input
    //output the average using call be reference
    
    void stddevcalc(ifstream& receive, double average, double& standarddeviation);
    //ifstream has been opened, ready to take input. Average has been calculated
    //output standard deviation using call by reference
    
    int main ()
    {
    ifstream receive;
    ofstream deliver;
    char yorn; //for yes or no answer to repeat calculations
    
    cout << "AVERAGE AND STANDARD DEVIATION\n\n"; //title
    
    
    do
    {
    double standarddev=0, avg=0, abc=0; //declare the variables to be changed by call by reference, and also abc. abc is used so that the first if statement is only run after the first time it fails
    char fileget[22], filegive[22]="RESULTS.txt", pause; // set variable for file to be calculated and file to be sent to. I use a dummy char variable because I am running in an IDE and it keeps the output on the screen
    
    do
    {
    if(abc > 0) //so error message is output to screen after the first file is not found or there is an error
    {
    cout << "receive/fileget/first open " << fileget << " - FAILED to OPEN\n";
    }
    cout << "File name: "; //so the user can specify which file to use for calculations
    cin >> fileget;
    
    receive.open(fileget);
    
    abc++; //as stated, this is to control error output
    }while(receive.fail()); //allows user to re-input the file name until he gets it right ERROR HERE
    
    cout << "receive ifstream has been opened for first calculation\n";
    
    deliver.open(filegive, ios::app); //it is set so that it can be appended so that multiple calculations can be done and the user history is kept ERROR HERE
    if (deliver.fail())
    {
    cout << "deliver/filegive/first open - FAILED to OPEN\n";
    cin >> pause;
    exit(1);
    }
    
    cout << "deliver ofstream has been opened\n";
    
    avgcalc(receive, avg); //average calculation here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    deliver << "--- For File " << fileget << " ---\nAVG: " << avg << endl; //output the average results to the file
    
    receive.close();
    
    cout << "receive ifstream has been closed\n";
    
    receive.open(fileget); //ifstream is closed and re-opened so that the numbers can be read through again by the next function
    if(receive.fail())
    {
    cout << "receive/fileget/second open " << fileget << " - FAILED to OPEN\n";
    deliver << "Standard Deviation Calculation ERROR\n\n"; //this will output to the file to show that an error occured ERROR HERE
    cin >> pause;
    exit(1);
    }
    
    cout << "receive ifstream has been opened for second calculation\n";
    
    stddevcalc(receive, avg, standarddev); //standard deviation calculation here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    deliver << "STANDARD DEV: " << standarddev << endl << endl; //standard deviation is added to the file here
    
    receive.close();
    
    cout << "receive ifstream has been closed\n";
    
    cout << "Calculations Complete and Sent to " << filegive << "\n\nAgain?\n"; //prompt for another loop
    cin >> yorn;
    }while((yorn != 'n')||(yorn != 'N'));
    
    return 0;
    }
    
    
    //~~~~~~~\~~~~~~~~~~\~~~~~~~~~\~~~~~~~~~~~~\~~~~~~~~~\~~~~~~~~~~\~~~~~~~~~~~\~~~~~~~~~~~\~~~~~~~~~~\~~~~~~~~~~~~\~~~~~~~~~~~~~~~~~\~~~~~~~~~~~~~~\~~~~~~~~\~~~~~~~~\~~~~~~~~~
    
    
    void avgcalc(ifstream& receive, double& average)
    {
    double number, total=0, count=0;
    
    while(receive >> number) // to keep the loop going while there is data left in the file
    {
    receive >> number; //get next number from file
    total+=number; // add number to total
    count++; //increment count
    }
    
    average = (total / count); //calculate average
    }
    
    void stddevcalc(ifstream& receive, double average, double& standarddeviation)
    {
    double number, add=0, count=0, sqr=0, sub=0;
    
    while(receive >> number)
    {
    receive >> number;
    sub = number - average; //first step of calculating standard deviation
    sqr = sub*sub; //second step of calculating standard deviation. square the first step
    add+=sqr; // third step. add the squared terms up
    count++;
    }
    
    standarddeviation = sqrt(add/count); //find the average of the squared terms and square root it --> standard deviation
    }

  6. #6
    Join Date
    Apr 1999
    Posts
    27,418

    Re: C++ using fstream to calculate average and standard deviation

    Quote Originally Posted by wsad597 View Post
    Here it is with a code tag.
    That won't help if your code is horribly formatted as it is now.

    Please properly format your code, i.e. use indentation. Right now, everything is flushed to the left, and that makes it almost impossible to follow your code.
    Code:
    //calculate average and standard deviation of a specified file and repeat as needed
    //
    //
    //possible logic error location are marked by the phrase ERROR HERE
    //fails to open file the second tiime every time. Never been able to calculate standard deviation. Also it doesnt tell file that an error occurred
    //also, once you input the wrong file name once it says all other file names are incorrect too
    //
    
    
    #include <iostream>
    #include <fstream>
    #include <cstdlib> //for key word - exit
    #include <cmath> // for keyword - sqrt
    
    using namespace std;
    
    void avgcalc(ifstream& receive, double& average);
    //ifstream has been opened, ready to take double input
    //output the average using call be reference
    
    void stddevcalc(ifstream& receive, double average, double& standarddeviation);
    //ifstream has been opened, ready to take input. Average has been calculated
    //output standard deviation using call by reference
    
    int main ()
    {
        ifstream receive;
        ofstream deliver;
        char yorn; //for yes or no answer to repeat calculations
    
        cout << "AVERAGE AND STANDARD DEVIATION\n\n"; //title
    
    
        do
        {
            double standarddev=0, avg=0, abc=0; //declare the variables to be changed by call by reference, and also abc. abc is used so that the first if statement is only run after the first time it fails
            char fileget[22], filegive[22]="RESULTS.txt", pause; // set variable for file to be calculated and file to be sent to. I use a dummy char variable because I am running in an IDE and it keeps the output on the screen
    
            do
            {
                if (abc > 0) //so error message is output to screen after the first file is not found or there is an error
                {
                    cout << "receive/fileget/first open " << fileget << " - FAILED to OPEN\n";
                }
                cout << "File name: "; //so the user can specify which file to use for calculations
                cin >> fileget;
    
                receive.open(fileget);
    
                abc++; //as stated, this is to control error output
            }while (receive.fail()); //allows user to re-input the file name until he gets it right ERROR HERE
    
            cout << "receive ifstream has been opened for first calculation\n";
    
            deliver.open(filegive, ios::app); //it is set so that it can be appended so that multiple calculations can be done and the user history is kept ERROR HERE
            if (deliver.fail())
            {
                cout << "deliver/filegive/first open - FAILED to OPEN\n";
                cin >> pause;
                exit(1);
            }
    
            cout << "deliver ofstream has been opened\n";
    
            avgcalc(receive, avg); //average calculation here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
            deliver << "--- For File " << fileget << " ---\nAVG: " << avg << endl; //output the average results to the file
    
            receive.close();
    
            cout << "receive ifstream has been closed\n";
    
            receive.open(fileget); //ifstream is closed and re-opened so that the numbers can be read through again by the next function
            if (receive.fail())
            {
                cout << "receive/fileget/second open " << fileget << " - FAILED to OPEN\n";
                deliver << "Standard Deviation Calculation ERROR\n\n"; //this will output to the file to show that an error occured ERROR HERE
                cin >> pause;
                exit(1);
            }
    
            cout << "receive ifstream has been opened for second calculation\n";
    
            stddevcalc(receive, avg, standarddev); //standard deviation calculation here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
            deliver << "STANDARD DEV: " << standarddev << endl << endl; //standard deviation is added to the file here
    
            receive.close();
    
            cout << "receive ifstream has been closed\n";
    
            cout << "Calculations Complete and Sent to " << filegive << "\n\nAgain?\n"; //prompt for another loop
            cin >> yorn;
        }while ((yorn != 'n')||(yorn != 'N'));
    
        return 0;
    }
    
    
    //~~~~~~~\~~~~~~~~~~\~~~~~~~~~\~~~~~~~~~~~~\~~~~~~~~~\~~~~~~~~~~\~~~~~~~~~~~\~~~~~~~~~~~\~~~~~~~~~~\~~~~~~~~~~~~\~~~~~~~~~~~~~~~~~\~~~~~~~~~~~~~~\~~~~~~~~\~~~~~~~~\~~~~~~~~~
    
    
    void avgcalc(ifstream& receive, double& average)
    {
        double number, total=0, count=0;
    
        while (receive >> number) // to keep the loop going while there is data left in the file
        {
            receive >> number; //get next number from file
            total+=number; // add number to total
            count++; //increment count
        }
    
        average = (total / count); //calculate average
    }
    
    void stddevcalc(ifstream& receive, double average, double& standarddeviation)
    {
        double number, add=0, count=0, sqr=0, sub=0;
    
        while (receive >> number)
        {
            receive >> number;
            sub = number - average; //first step of calculating standard deviation
            sqr = sub*sub; //second step of calculating standard deviation. square the first step
            add+=sqr; // third step. add the squared terms up
            count++;
        }
    
        standarddeviation = sqrt(add/count); //find the average of the squared terms and square root it --> standard deviation
    }
    Do you now see how this is formatted correctly?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 8th, 2011 at 11:42 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center