Get a value from a data

My sibling ask me to find error in his coding. He want to find smallest value he get highest instead. i already try figure out. i spend 2 hour try changing his coding but still could fix it. this is his coding :

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
 void smallest (ifstream & theFile) 
{
 
        int lowest1 ;
        int lowest2 ;
        int i;

        for (int a {}, b {}, c {}; theFile >> a >> b >> c;)
    {
         lowest1 = b;
         lowest2 = c;
         

        for ( i = 1 ; i < 20 ; i++) // 20 rows
        {
                if (b < lowest1)
                   lowest1 = b;

                if (c < lowest2)
                   lowest2 = c;
        }
    }
        
        cout << lowest1 << '\n';
        cout << lowest2 << '\n';
     

}
You don't need the for loop L14

Perhaps (not tried):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void smallest(ifstream& theFile)
{
	int lowest1 {std::numeric_limits<int>::max()};
	int lowest2 {std::numeric_limits<int>::max()};

	for (int a {}, b {}, c {}; theFile >> a >> b >> c; ) {
		if (b < lowest1)
			lowest1 = b;

		if (c < lowest2)
			lowest2 = c;
	}

	cout << lowest1 << '\n';
	cout << lowest2 << '\n';
}


Well, each time you loop ... you read in new values of a, b, c ... and assign new values of lowest1 and lowest2 to b and c ... then you check 19 times whether something that can never happen does, nevertheless, happen.

So, if that filestream is positioned at the beginning of the file then you are probably going to write out the last items read successfully.
Last edited on
seeplus, i try the coding but it said that numeric_limits is not a member of 'std' and other 6 error
lastchance, i can't really understand what you trying to tell me . sorry
@Hyung. You need to have #include <limits> at the top of the code (where the other #include statements are). Your code posted above isn't a complete program - and I just changed the function.

This is a complete program:

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 <limits>
#include <iostream>
#include <fstream>
using namespace std;

void smallest(ifstream& theFile)
{
	int lowest1 {std::numeric_limits<int>::max()};
	int lowest2 {std::numeric_limits<int>::max()};

	for (int a {}, b {}, c {}; theFile >> a >> b >> c; ) {
		if (b < lowest1)
			lowest1 = b;

		if (c < lowest2)
			lowest2 = c;
	}

	cout << lowest1 << '\n';
	cout << lowest2 << '\n';
}

int main()
{
	// program code goes here
}

Last edited on
okay, i got it . but what does numeric limit do ?
and if i want to use the int i for rows , should i eliminate the for loop ?
but what does numeric limit do ?


In the code above, it returns the maximum possible value for the type int. As we've looking for the smallest value, first set the result to the largest Similarly, if looking for the largest value, first set the result to the smallest.

to use the int i for rows


Why - unless you want to know the row(s) for the largest values?
Registered users can post here. Sign in or register to post.