### Finding max and min from text file containing integers, square brackets, and commas.

I have been struggling with this code for quite some time. I have found a way to find the average using the same method. However when finding the max and min it is giving it to me for every line. I need to find max and min comparing numbers from the whole file.

Are there other ways I can stop C++ from interpreting square brackets as integers ??

Sample of the text file:
[407, 706, 513]
[150, 632, 367]
[616, 575, 393]
[701, 172, 545]
[311, 207, 284]
[210, 659, 535]
 ``123456789101112131415161718192021222324252627282930313233`` ``````void problem2(string s) { ifstream fin{s}; if (fin.is_open()) { char Brac1{}, comma1{}, comma2{}, Brac2{}; int first{}, second{}, third{}; int min{}, max{},x1{},x2{}; while (fin >> Brac1 >> first >> comma1 >> second >> comma2 >> third >> Brac2) { min=x1; max=x2; x1=first; x2=second; } if(x1 < second && x1< third) { min=x1; } if(x2 > first && x2 > third) { max=x2; } cout << min << ","<< max << '\n'; } } ``````

This is what I attempted so far.
Last edited on
We can't see your current code and therefore cannot tell what is wrong in it.

Overall:
 ```for each line in file for each number on line test min,max / add to sum / etc ```
To obtain the min/max numbers for the whole file, consider for unknown number of columns:

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include #include struct Data { int num {}; }; std::istream& operator>>(std::istream& is, Data& d) { if (is.peek() == '[') is.get(); char tmp {}; return is >> d.num >> tmp >> std::ws; } int main() { std::ifstream ifs("data.txt"); if (!ifs) return (std::cout << "Cannot open file\n"), 1; int minno {std::numeric_limits::max()}, maxno {std::numeric_limits::min()}; for (Data d; ifs >> d; ) { if (d.num > maxno) maxno = d.num; if (d.num < minno) minno = d.num; } std::cout << "Minimum is " << minno << "\nMaximum is " << maxno << '\n'; }``````

Last edited on
@cplusbeginner2222,
Does every line have precisely three integers on it? Or are there arbitrary numbers per line?
Yes it has three integers @lastchance
Last edited on
 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #include #include #include #include using namespace std; istringstream in( "[407, 706, 513]\n" "[150, 632, 367]\n" "[616, 575, 393]\n" "[701, 172, 545]\n" "[311, 207, 284]\n" "[210, 659, 535]\n" ); int main() { int minimum = numeric_limits::max(); int maximum = numeric_limits::min(); double sum = 0; int N = 0; char ch; int a, b, c; while( in >> ch >> a >> ch >> b >> ch >> c >> ch ) { sum += a + b + c; N += 3; minimum = min( { minimum, a, b, c } ); maximum = max( { maximum, a, b, c } ); } cout << "Sum = " << sum << '\n'; cout << "Average = " << sum / N << '\n'; cout << "Minimum = " << minimum << '\n'; cout << "Maximum = " << maximum << '\n'; }``````

 ```Sum = 7983 Average = 443.5 Minimum = 150 Maximum = 706```
Last edited on
Topic archived. No new replies allowed.