while (fin >> val)
{
fin >> val;
if (val < min)
val = min;
else
val = max;
}
Changemin and/or max, not val.
while ( fin >> val )
{
if ( val < min )
{
min = val;
}
if ( val > max )
{
max = val;
}
}
It might also be helpful to post the contents of"inFile.txt".
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>i tried the way u suggested me but it still gives same output
Odd.
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream fin("inFile.txt");
int min, max, val;
if ( fin >> val )
{
min = max = val;
while ( fin >> val )
{
if ( val < min )
{
min = val;
}
if ( val > max )
{
max = val;
}
}
}
cout << "Largest value is " << max << ", smallest value is " << min << "\n";
return 0;
}
/* my output
Largest value is 100, smallest value is 45
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>fin >> isFirst;
I think you mean:
fin>> val;
isFirst doesn't strike me as a data variable, it seems more like a status variable. You don't even need it. Oddly enough, I didn't bother looking at Dave's code, but my solution is only slightly different (but it avoids undefined behavior in the case of an empty or non-existent file):
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream fin("inFile.txt");
int val;
if (fin >> val) {
int min, max;
min = max = val;
while (fin >> val)
{
if (val < min)
min = val;
if (val > max)
max = val;
}
cout << "Largest value is " << max << ", smallest value is " << min << "\n";
}
return 0;
}
Great minds think alike, I suppose.
>i tried every possible way u suggested me and it still doesnt work.
Then instead of looking at the suggestions and implementing them they wayyou think they should be done, actually try them as they are written. In my experience, Dave doesn't post untested code.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>here is what i did but it gives average = 77
Um...what's the problem with getting the correct answer?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
The average is 76.4666.
desidude, since your sum and count are modified after the while loop, only the last value of your numbers gets added to sum, and count gets incremented once, from zero to one. And 77 is your last value.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
Just put the
sum = sum+val;
count=count++;
inside your while loop, and it shuld work.
zyruz
Junior Poster in Training
60 posts since Jun 2005
Reputation Points: 10
Solved Threads: 5
Don't forget to account for the first value:
if ( fin >> val )
{
sum += val;
++count;
min = max = val;
while ( fin >> val )
{
if ( val < min )
{
min = val;
}
if ( val > max )
{
max = val;
}
sum += val;
++count;
}
}
On a side note, this is very, very wrong:
count=count++;
It modifies count twice between sequence points, which is undefined.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401