How do I determine the highest and lowest values from a text file?
For part of my assignment I have to do what the title says. Here's the file's contents.
1 2 3 4 5 6 7 8 9 10 11 12
|
95
100
120
130
135
145
155
185
190
160
130
120
|
What exactly do I have to do to be able to determine my highest and lowest values? I'll post what I've written so far if needed.
.
Last edited on
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 26
|
#include <iostream>
#include <fstream>
#include <limits>
using namespace std;
int main()
{
int low = numeric_limits<int>::max();
int high = numeric_limits<int>::min();
ifstream fin("text.txt");
if(!fin)
return 1;
int n;
while(fin >> n)
{
if(n > high)
high = n;
if(n < low)
low = n;
}
cout << "Low: " << low << " High: " << high << '\n';
}
|
Topic archived. No new replies allowed.