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 }