looping

i am stuck with looping problem
program reads text file with some random positive and negative numbers (there is only twelve numbers)
and it needs to find highest number and lowest number
i need to use if statement where it checks for not end of file
and sets largest to number and sets smallest to number
something like this

if(not end of file)
set largest to number
set smallest to number
endif

numbers are read properly since i got everything else working correctly, like how many positive and negative, count, average, even and odd
i just dont understand how to get largest and smallest part
i would appreciate some help
You would need to keep track as you go. Each time you read a new number, check to see if it is smaller or larger than the smallest or largest so far.
Does it have to be an if loop? I would suggest using a while loop, of course the same concept applies.

1
2
3
4
while(infile)
{
    //operations here
}
I hope you're using if statements and not if preprocessor directives.
it has to be if statement
like pseudo code i have in first post
this is what i got so far
but no luck

if (!inFile.eof() )
{
largestNumber = number;
smallestNumber = number;
{
if (number > largestNumber)
largestNumber = number;
}
{
else if (number < smallestNumber)
smallestNumber = number;
}
}
Last edited on
That's pretty much right, except you have an unconditional assignment of largestNumber and smallestNumber that will make your results incorrect. You only want to do that assignment the first time you read a number.

And I think this would be annoying to implement as anything but a loop...you would end up with several repeated reads and copy & pasted if() statements and such.
Topic archived. No new replies allowed.