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]
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
27
28
29
30
31
32
33
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:

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
27
28
29
30
31
32
33
34
35
36
37
#include <fstream>
#include <iostream>
#include <limits>

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<int>::max()}, maxno {std::numeric_limits<int>::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
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
27
28
29
30
31
32
33
34
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <limits>
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<int>::max();
   int maximum = numeric_limits<int>::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.