Still doesn't work.

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've posted this a couple times, but none of the responses, which are appreciated, have worked. 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;
}
}
}
if(max<data[11])max=data[i];

I think you meant:

if(max<data[i])max=data[i];

Maybe that'll work for you.

Also, I don't understand the last block of braces you have. After if(file_in.eof())break;

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
#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;
          }
     }
}
closed account (z05DSL3A)
There is so much wrong I don't know where to start but just a few things

your data variable is not initialised
you read the line into input_line but don't put it into data (I assume that is where you want it)
if(file_in.eof())break; so do not go on to the remainder of the code?
for(i=1; i<=max; i++); will do nothing with the ; at the end.
Topic archived. No new replies allowed.