How do you find the min and max of user input number?

Feb 6, 2022 at 2:34am
I've been googling all day and reading through my book, but I just can't seem to find the answer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{


    double grade1;
    double grade2;
    double grade3;
    double grade4;
    double grade5;
    double avg_grade;




    //Input:

    cin >>grade1;
    cin >>grade2;
    cin >>grade3;
    cin >>grade4;
    cin >>grade5;
    cout<<"Min   =   "<<min(grade1, grade2, grade3, grade4, grade5);



now when I use
 
    cout<<"Min  =    "<<min(20,2)<<endl;


I get an output of 2. Is there a way to make this work for user input
Last edited on Feb 6, 2022 at 2:35am
Feb 6, 2022 at 2:44am
This will require a loop.

Each time through the loop, check to see if the element is less than (or greater than) your current min (or max).

You should also be using an array or vector for multiple variables of the same type.

 
    double grades[5];

Hope this helps.
Feb 6, 2022 at 3:05am
You can use this idea:
1
2
3
4
5
6
7
double a, b, c, d, e; 
double my_min;
std::cin >> a >> b >> c >> d >> e; 
my_min = min(a, b);
my_min = min(my_min, c);
my_min = min(my_min, d);
my_min = min(my_min, e);

Feb 6, 2022 at 5:42am
for user/file/network/device/etc inputs track this kind of data as it comes in rather than get them all and seek it later.
Feb 6, 2022 at 10:54am
You were close with L22! You need:

 
cout << "Min   =   " << min ({ grade1, grade2, grade3, grade4, grade5 });


The grades are put within {} to create an initialiser list.
Topic archived. No new replies allowed.