Quote Originally Posted by Paul McKenzie View Post
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





Alright. Thanks for the pro-tip.

As I said I'm new to this so I guess it didn't cross my mind that that was possible.

Makes more sense now.


Thanks for formatting it for my by the way.




So my main problem is with logic and possibly syntax though. Does anyone have any ideas?