Compile but not Execute

Geany does not indicate any errors. It compiles successfully, but does not execute.





#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <math.h>
#include <cstdlib>


using namespace std;
int main()


{
ofstream output_file;
output_file.open("output.txt");
if (output_file.fail()) {
cout << "\n The output file was not successfully opened"
<< "\n Please check that the path currently exists." <<endl;
exit(1);
}


ifstream input_file;

input_file.open("data51.txt");
if (input_file.fail()) {
cout << "\n The input file was not successfully opened"
<< "\n Please check that the path currently exists." <<endl;
exit(1);
}

float x, i, tot=0, count=0, standev=0, sumDiffSqr=0, min=0, max=0;
float avg=0;

ifstream myfile;
myfile.open("data51.txt");
myfile>>x;


{

while (myfile)



{
for (i = 0; i < count; i++)
{

if (x < min)
{
min = x;
}
else if (x > max)
{
max = x;
}
}

count++;
tot=tot + x;
}
if (count > 0)



myfile.close();
avg=tot/(float)count;

{
for (i = 0; i < count; i++)
{
sumDiffSqr = sumDiffSqr + pow((i - avg), 2);
}

standev = sqrt(sumDiffSqr / count);

}


output_file << "Number of numbers is: " << count << endl;
output_file << "Total of numbers is: " << tot << endl;
output_file<< "Average of numbers is: " << avg << endl;
output_file << "Max of numbers is: " << max << endl;
output_file << "Min of numbers is: " << min << endl;
output_file << "Standard Deviation of numbers is: " << standev << endl;

output_file.close();
input_file.close();

return 0;

}
}
What if myfile was not open correctly?
Also: while (myfile). If myfile was in valid state when you enter the loop, it will remain that way forever, as nothing change its state inside loop. So you got an infinite loop.
Last edited on
Topic archived. No new replies allowed.