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
}
Re: C++ using fstream to calculate average and standard deviation
nice post, thanks for sharing this usefull information
_______________________
Rakeback
Re: C++ using fstream to calculate average and standard deviation
Quote:
I'm sorry about the formatting if it doesn't work on your screen.
That's why we have code tags.
Re: C++ using fstream to calculate average and standard deviation
Quote:
Originally Posted by
Skizmo
ahhhhhh. Thank you.
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
}
Re: C++ using fstream to calculate average and standard deviation
Quote:
Originally Posted by
wsad597
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
Re: C++ using fstream to calculate average and standard deviation
Quote:
Originally Posted by
Paul McKenzie
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?
Re: C++ using fstream to calculate average and standard deviation
Once the failbit gets set on a stream, you need to clear it before
you can do anything else with the stream.
So, in your section, if the file can not be opened, the failbit gets set.
All attempts to open a file using that stream will fail.
Use the clear() member function to clear the flags for the stream.
Code:
cin >> fileget;
receive.clear();
receive.open(fileget);
I did not look at the rest of your code, but it is probably the same
problem.
Re: C++ using fstream to calculate average and standard deviation
Quote:
Originally Posted by
Philip Nicoletti
Once the failbit gets set on a stream, you need to clear it before
you can do anything else with the stream.
So, in your section, if the file can not be opened, the failbit gets set.
All attempts to open a file using that stream will fail.
Use the clear() member function to clear the flags for the stream.
Code:
cin >> fileget;
receive.clear();
receive.open(fileget);
I did not look at the rest of your code, but it is probably the same
problem.
Oh! Well that makes sense. I guess I was thinking it was more of a variable and less of a flag.
Thank you so much.