fstream secondfile;
secondfile.open("a3.txt");
if (!secondfile)
{
cout << "Cannot open file" << endl;
return 1;
}
int i;
int a;
int b;
int max;
int min;
for(i = 0; i <= count; i++)
{
secondfile >> values;
if(!secondfile)
break;
cout << values << endl;
values >> a, b;
if(values < a)
max = a;
elseif (values > a)
min = a;
}
cout << max << endl;
cout << min << endl;
system("Pause");
}
well the problem is not know how and what values I have to compare to get the max and min.
my data is a pety six numbers 96 200 196 19 974 99
all I have to do is open the file and run it through a for loop, finding the sets max and min.
1 2 3 4 5 6
values >> a, b;
if(values < a)
max = a;
elseif (values > a)
min = a;
i know that values >> a, b; is wrong, but it was worth a shot to try it.
now i am posting here as it "seems" i have exhausted all known resources.
it gives me either all zeros, or one zero and a rather large number
I think the basic problem is that you do not initialize min and max. I don't have your file, or the rest of your code, so it is hard to tell exactly what is going on. I would try something like this:
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
fstream secondfile;
secondfile.open("a3.txt");
if (!secondfile)
{
cout << "Cannot open file" << endl;
return 0;
}
int a, b, max, min;
secondfile >> a ; // Need at least 1 number in file.
if (!secondfile.eof())
secondfile >> b;
else
b = a;
(a<b) ? (max = b,min = a) : (max = a,min = b);
cout << "The first two numbers are: "<< a << " " << b << endl;
while (!secondfile.eof())
{
secondfile >> a;
cout << "The next number is: "<< a << endl;
if(a<min)
{
min = a;
}
elseif(a>max)
{
max = a;
}
}
secondfile.close();
cout << endl << "The max is: " << max << endl;
cout << "The min is: " << min << endl;
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main ( )
{
fstream myfile;
myfile.open ("a3.txt");
if (!myfile)
{
cout << "Cannot open file" << endl;
return 1;
}
int values;
int average;
int count;
int total;
total = 0;
count = 0;
cout << "The first reading of the file, using a while loop, reads:\n";
while(myfile >> values)
{
cout << values << endl;
total = total + values;
count++;
}
cout << "The total sum of these numbers are: " << total << endl;
average = total / count;
cout << "The average of this number set is: " << average << endl;
myfile.close();
fstream secondfile;
secondfile.open("a3.txt");
if (!secondfile)
{
cout << "Cannot open file" << endl;
return 1;
}
int i;
int a;
int max;
int min;
for(i = 0; i <= count; i++)
{
secondfile >> a;
if(a > max)
{
max = a;
}
elseif(a < min)
{
min = a;
}
}
secondfile.close();
cout << max << endl;
cout << min << endl;
system("Pause");
}
Problem is that you use max and min in expressions without formal initialization. So whatever data is in the space it allocates for the variable is used. It could be garbage.
Just set max and min to -10000, and 10000 respectively.
wolfgang yer a monster
thanks so much.
never occurred to me that then needed to me initialized to a VALUE. sheesh.
but thank you so much.
crazy how that one thing can trip the whole thing up.