Help With File Input/Output

Hello everyone.
This is my first semester programming trying to become a Computer Engineer. I kinda suck at this.
I am currently confused on how to get the highest and lowest number from a list of 7 numbers for a File Output. Lets say i have 165 19 654 816 654 987 324. How would i get the 987 for the highest and the 19 as the lowest? Those numbers are not fixed numbers, i need to be able to input any combination of numbers and still be able to get the highest and lowest numbers from the list of 7 numbers. I would really appreciate the help. Thank you.
I'm not very efficient in working with files in c++, so I will go ahead and show you how I will do this in c. The ideas are very much alike and easily transferable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstdlib>
#include <limits>
#include <cstdio>

int main()
{
   int max(std::numeric_limits<int>::min()), min(std::numeric_limits<int>::max()), dummy;
   //Set max to a very low value
   //Set min to a very high value
   //http://www.cplusplus.com/reference/limits/numeric_limits/

   FILE* in = fopen("myfile.txt", "r+");
   
   while(!feof(in) and fscanf(in, "%d ", &dummy))
   {
      max = dummy > max ? dummy : max;
      min = dummy < min ? dummy : min;
   }
   fclose(in);
   
   std::cout << "Max value read is: " << max << std::endl << "Min value read is: " << min << std::endl;
   
   return 0;
}
Last edited on
Topic archived. No new replies allowed.