My Min and Max Values are displaying as '0.'

Hello, I am currently working on a project that requires the maximum and minimum of four randomly entered numbers to be outputted. I seem to be on the right track; However, after entering four random numbers the Max and Min values are displayed as zero. I am unaware of what I am doing wrong.

#include<iostream>
using namespace std;

int main(){

int numbr1 = 0; int numbr2 = 0;
int numbr3 = 0; int numbr4 = 0;
int maxValue = INT_MAX; int minValue = INT_MIN;

cout<<"***************"<<endl;
cout<<"Program 5 Min Max"<<endl;
cout<<"***************"<<endl;
cout<<"Please give four numbers: "<<endl;

{
if(numbr1<maxValue && numbr1>minValue);
maxValue = numbr1;
minValue = numbr1;
cout<<"Number 1: ";
cin>>numbr1;
}
{
if(numbr2<maxValue && numbr2>minValue);
maxValue = numbr2;
minValue = numbr2;
cout<<"\nNumber 2: ";
cin>>numbr2;
}
{
if(numbr3<maxValue && numbr3>minValue);
maxValue = numbr3;
minValue = numbr3;
cout<<"\nNumber 3: ";
cin>>numbr3;
}
{
if(numbr4<maxValue && numbr4>minValue);
maxValue = numbr4;
minValue = numbr4;
cout<<"\nNumber 4: ";
cin>>numbr4;
}

cout<<"Max Value: "<<maxValue<<endl;
cout<<"Min Value: "<<minValue<<endl;

return 0;
}
Last edited on
You've written:
if(numbr2<maxValue && numbr2>minValue);
But if statements must not be terminated with a semicolon, so that if statement is obsolete as it doesn't have a corresponding block of statements attached to it.

The correct way to write an if statement:
1
2
3
4
5
6
7
if (condition) { 
   statement 1;
   statement 2;
   ..
   ..
   statement n;
}

http://www.cplusplus.com/doc/tutorial/control/

The code you wrote was interpreted by the compiler as so:
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
38
39
#include<iostream>
using namespace std;

int main() {

    int numbr1 = 0; int numbr2 = 0;
    int numbr3 = 0; int numbr4 = 0;
    int maxValue = INT_MAX; int minValue = INT_MIN;

    cout << "***************" << endl;
    cout << "Program 5 Min Max" << endl;
    cout << "***************" << endl;
    cout << "Please give four numbers: " << endl;

    maxValue = numbr1;
    minValue = numbr1;
    cout << "Number 1: ";
    cin >> numbr1;

    maxValue = numbr2;
    minValue = numbr2;
    cout << "\nNumber 2: ";
    cin >> numbr2;

    maxValue = numbr3;
    minValue = numbr3;
    cout << "\nNumber 3: ";
    cin >> numbr3;

    maxValue = numbr4;
    minValue = numbr4;
    cout << "\nNumber 4: ";
    cin >> numbr4;

    cout << "Max Value: " << maxValue << endl;
    cout << "Min Value: " << minValue << endl;

    return 0;
}


The last time you assigned maxValue and minValue was in these two lines:
1
2
    maxValue = numbr4;
    minValue = numbr4;

But the value of numbr4 was 0, as you had initialized it to be so at the beginning. Hence maxValue and minValue get assigned 0.

Pseudocode of how you should be doing this:
--> Input the numbers
--> minValue = first number, maxValue = first number
--> check whether second number is larger than first number
if true - assign maxValue the value of second number
--> check whether second number is smaller than first number
if true - assign minValue the value of second number
=> Do the same procedure for the rest of the two remaining numbers.
--> Print results.
The reason for this is you assigned the value '0' to your variables and then performed a comparison before you asked the user to input a value. therefore the max and min value will always be '0'. Try placing your user prompt before the if statements.
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
using namespace std; 

int main(){

int numbr1 = 0; int numbr2 = 0; 
int numbr3 = 0; int numbr4 = 0; 
int maxValue; int minValue;

cout<<"***************"<<endl;
cout<<"Program 5 Min Max"<<endl;
cout<<"***************"<<endl;
cout<<"Please give four numbers: "<<endl;



cout<<"Number 1: "; 
cin>>numbr1;
maxValue = numbr1;
minValue = numbr1;   

cout<<"\nNumber 2: ";
cin>>numbr2;
  if(numbr2 > maxValue) {
     maxValue = numbr2;   
  }
  if(numbr2 < minValue) {
     minValue = numbr2;   
  }

cout<<"\nNumber 3: ";
cin>>numbr3; 
  if(numbr3 > maxValue) {
     maxValue = numbr3;   
  }
  if(numbr3 < minValue) {
     minValue = numbr3;   
  }

cout<<"\nNumber 4: ";
cin>>numbr4;
if(numbr4 > maxValue) {
     maxValue = numbr4;   
  }
  if(numbr4 < minValue) {
     minValue = numbr4;   
  }


cout<<"Max Value: "<<maxValue<<endl;
cout<<"Min Value: "<<minValue<<endl;

return 0;
}
I appreciate your help guys!
Topic archived. No new replies allowed.