Can't get max

I can't get this program to work right. It reads and displays the file it's supposed to, but it won't figure out the max and display it. I'm a newb at this so any help is appreciated.

#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;
}
}
}
better formatting:

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
#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;
		}
	}
}


one thing to remember, and I'm not sure how helpful this is, for arrays like data[11] the index positions aren't 1 - 11, but actually 0 - 10. So on line 20, you could change it to for(i=0; i<11 && !file_in.eof(); i++)

also, in this chunk:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
		}
	}


I suggest that you change the while(1) to something more useful, maybe use a bool?
Maybe something like:
1
2
3
4
5
6
7
8
9
10
11
12
bool reloop == true;
...
while(reloop == true)
	{
		...
		if(file_in.eof()); //also the 'break' that was here could be part of your problem
		{
			...
			reloop == false;
			...
		}
	}


no guarantees that my advice will be right, in fact I doubt much of it is XD so somebody correct me where I'm wrong
Additionally:

1
2
3
double max = data[11];
for(i=1; i<=max; i++);
if(max<data[11])max=data[i];


Line 2 has an extra semicolon at the end of the line. That means that the for loop will repeat doing nothing rather than the next intended statement. On line 3, I think you want your index to be i.
Topic archived. No new replies allowed.