Unhandled Exception
Apr 24, 2008 at 2:01am UTC
I am trying to run a program and after the output screen comes up, i get this extra message box that says "Unhandled Exception in....:Access violation". How can i fix that? Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <fstream>
#include <cmath>
#include <iostream>
using namespace std;
double sum(double the_standard_Deviation[], int size);
int main ()
{
ifstream inputfile;
double N;
int size;
const int Num_List = 1000;
double summ;//, avg;
inputfile.open("program8.txt" );
if (inputfile.fail())
{
cout << "Error: could not open program8.txt" << endl;
exit (EXIT_FAILURE);
}
double standDeviation [Num_List];
inputfile >> N;
while (!inputfile.eof() && (size < Num_List))
{
standDeviation[size] = N;
size++;
cout<< N<<endl;
inputfile >> N;
}
summ = sum(standDeviation, size);
cout<< "SUM: " << N << " " << size << " " << summ << endl;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
inputfile.close();
system("Pause" );
return 0;
}
My error shows an arrow at the line of:
standDeviation[size] = N;
Apr 24, 2008 at 2:26am UTC
You never initialize size.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <fstream>
#include <cmath>
#include <iostream>
using namespace std;
double sum(double the_standard_Deviation[], int size);
int main ()
{
ifstream inputfile;
double N;
//some random number...
int size;
const int Num_List = 1000;
double summ;//, avg;
inputfile.open("program8.txt" );
if (inputfile.fail())
{
cout << "Error: could not open program8.txt" << endl;
exit (EXIT_FAILURE);
}
double standDeviation [Num_List];
inputfile >> N;
while (!inputfile.eof() && (size < Num_List))
{
//Using size here with that random number in place == Bad Stuff Will Happen
standDeviation[size] = N;
size++;
cout<< N<<endl;
inputfile >> N;
}
summ = sum(standDeviation, size);
cout<< "SUM: " << N << " " << size << " " << summ << endl;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
inputfile.close();
system("Pause" );
return 0;
}
Apr 24, 2008 at 2:42am UTC
I had a feeling that it was going to be such an easy mistake. Thank you.
Topic archived. No new replies allowed.