Why it can't find max value?

I don't understand why it can't find correct max value.



///Lab 13Problem 3
#include<iostream>
using namespace std;
#include<fstream>
ifstream getdata;
ofstream writedata;
int main ()
{
//Define var
float omsk[5], max;
int i,choice;
//Data input
cout<<"Please choice your data\n";
cout<<"1. Data 1\n";
cout<<"2. Data 2\n";
cout<<"\nData ";
cin>>choice;
if (choice==1)
{
getdata.open("Data 1.cpp",ios::in);
}
else if (choice==2)
{
getdata.open("Data 2 .cpp",ios::in);
}
//The maximum value of array
getdata>>omsk[0];
max=omsk[0];
cout<<omsk[0];

for(i=0;i<=4;i=i+1)

{getdata>>omsk[i];
if (max<omsk[i])

{
max=omsk[i];
}
cout<<"\n";
cout<<omsk[i];
}

cout<<"\n";
cout<<"Maximum value is "<<max;
getdata.close();
cout<<"\n";
system("pause");
}






That what i have for answer

Please choice your data
1. Data 1
2. Data 2

Data 1
19
24
1
10
14
7.65668e+033
Maximum value is 7.65668e+033
Press any key to continue . . .
Last edited on
Does "Data 1.cpp" contain 6 numbers?
You read 6 floats from it, and I suspect that the sixth one is just garbage (equals 7.65668e+033) and which is larger than your normal data.
Last edited on
No," Data 1" contains 5 numbers. (19,24,1,10,14).
Last edited on
Then read only 5 numbers. You can do it by modifying for(i=0;i<=4;i=i+1) to for(i=0;i<4;i=i+1).
Then read only 5 numbers. You can do it by modifying for(i=0;i<=4;i=i+1) to for(i=0;i<4;i=i+1).
Thank you!! it works!!
Topic archived. No new replies allowed.