I'm currently working on a program for my computer science class. We need to
read several lines containing integers from a file and output to another file.
Each line ends with a negative number (the sentinel). For testing purposes, I'm
able to write the code that reads the input data and then sends that data to the
output file. However, the assignment asks us to find the minimum and maximum
number from each line and then print only those numbers to the file.
For whatever reason, I'm having trouble figuring-out how to find the minimum and
maximum numbers. The number of input integers may vary. It seems there should
be a way to compare the integers as they're read. For instance, something like:
1. Read the first number
2. Read the second number
3. compare 1st and 2nd number
4. Read the third number
5. etc., etc.
6. When a negative number is read, terminate the loop
Could anyone give me some guidance as to what I'm missing?
Say my input is: 11 55 44 88 12 90 89 55 100 -5.
The loop I've written so far prints:
The Maximum number is: 100
The Minimum number is: -5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
while (inData)
{
int minimum = 0, maximum = 0, num = 0;
while (num >= 0)
{
inData >> num;
if (num < minimum)
minimum = num;
if (num > maximum)
maximum = num;
}
cout << "The Maximum Number is: " << maximum << endl;
cout << "The Minimum Number is: " << minimum << endl;
}
|