uninitialized local variable

Hi guys n gals,

I'm getting the "uninitialized local variable 'data' used" on line 26 and i can't figure out why. The program is supposed to read from a file, display it (which it does), and then display the largest number (which it doesn't). Any help would be appreciated.

Thanks

p.s. I also posted this in the c++ programming forum. Got 1 response which was cool, but it didn't really tell me what to do to fix my problem.

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int i;
char filename[81], input_line[81];
double data[11];
cout << "Enter the file name." << endl;
cin.getline(filename, 80);
ifstream file_in(filename);
if(!file_in)
{
cout << "Error! No file!";
return -1;
}
while(1)
{
for(i=1; i<=11 && !file_in.eof(); i++)
{
file_in.getline(input_line, 80);
cout << input_line << endl;
}
if(file_in.eof())break;
{
double max = data[11];
for(i=1; i<=max; i++);
if(max<data[11])max=data[i];
cout << "The maximum Magnitude is " << max;
file_in.close();
return 0;
}
}
}
Look at this: double data[11];
This means that data is an array of 11 doubles which can be accessed with indices from 0 to 10.

Now, look at this: for(i=1; i<=11 && !file_in.eof(); i++)
It should be like: for(i=1; i< 11 && !file_in.eof(); i++) (plain <, not <=)

And this: double max = data[11]; <- this should be double max = data[10];


There's also a problem with the loop finding the maximum element:
1
2
for(i=1; i<=max; i++); //<-I believe you wanted to write for(i=1; i<11; i++);
if(max<data[11])max=data[i]; //<-and this should be if(max<data[i])max=data[i]; 
I made those changes and not only do i still get the same error, but it only read 9 of the data points in the file it reads from instead of all 10.
Topic archived. No new replies allowed.