finding the highest value

whats wrong with this code ? when i run it ,it gives me the smallest amount not the largest one

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
int num1,num2;
cout<<"enter two number";
cin>>num1,num2;
if(num1>num2){cout<<"the highest value is :"<<num1;}
else if (num2>num1) { cout<<"the highest value is:"<<num2;
}
getch();

;return 0 ;
}
Hello and welcome :) Please always use code tags for all of your code. It's the <> button under the format section.

http://www.cplusplus.com/articles/jEywvCM9/

Your problem is this cin>>num1,num2; That's not what the coma operator does.

You want to use another >> instead. cin >> num1 >> num;

But in the future make all of it more clear

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Example program
#include <iostream>

using namespace std;

int main()
{
    int num1,num2;
    
    cout << "Enter first number: ";
    cin >> num1;
    cout << "\nEnter Second Number: ";
    cin >> num2;

    if(num1 > num2) 
    {
        cout << "\n\nThe highest value is: " << num1; 
    }
    else if(num2 > num1)
    {
        cout << "\n\nThe highest value is: " << num2;
    }
}
TarikNeaj answered the question pretty much.

If you want to go further, you can add an "else" statement just in case user enters 2 same values such that num1 == num2.

For example,
1
2
else
    std::cout << "\n\nThe numbers are equal." << std::endl;


With the final else statement, your codes covered all relational possibilities no matter what integers users enter.
Topic archived. No new replies allowed.