-6

In c++ I have am going through a online course and I have no idea where else to find the answer to this or good c++ websites to learn from. The code below is supposed to give the output: Both numbers are equal OR Greater value is a/b.

#include <iostream>    
#include <string>    
using namespace std;    
int main(int argc, char* argv[])    
{    
  int a,b,c=0;    
  cout<<"Enter value of 1st variable: ";    
  cin>>a;    
  cout<<"Enter value of 2nd variable: ";    
  cin>>b;    

  {
    if ( !(a == b));
    !true;

    cout << "Both are equal";
  }
  !false;

  {
    if (a>b)
      c = a; 
    if (b>a);
      c = b; 
    cout << "Greater value is " << c;
  }
}

I don't know any other ways to give the output I want.

So if the first input is 50 and the second is 50 I get the output Both are equalGreater value is 50

If the first is 50 and the second is 49 I get the output Both are equalGreater value is 49.
If the first is 50 and the second is 90 I get the output Both are equalGreater value is 90.

Bill
  • 14,257
  • 4
  • 43
  • 55

3 Answers3

1

You have several problems with basic C++ syntax.

An if statement should not normally end with a semicolon:

if ( !(a == b));

Should be:

if ( !(a == b))
//            /\ No semicolon here!

The braces for an if statement should go after the closing parenthesis. This:

{
if (!(a == b))
}

Should be:

if (!(a == b))
{
}

Neither of these lines do anything:

!true;
!false;

Extra braces are allowed, which is why your compiler is not complaining about them. Just because I can do this, doesn't mean I should:

int main()
{
  {
    {
      {
        // code here
      }
    }
  }
}
Bill
  • 14,257
  • 4
  • 43
  • 55
0

Apparently you don't know how to use if else correctly in c++. You can look them up e.g. here.

One correct way your program could look like:

#include <iostream>    
#include <string>    
using namespace std;
int main(int argc, char* argv[])
{
    int a, b, c = 0;
    cout << "Enter value of 1st variable: ";
    cin >> a;
    cout << "Enter value of 2nd variable: ";
    cin >> b;


    if (a == b){
        cout << "Both are equal";
    } else {
        if (a > b) {
            c = a;
        } else if (a < b){ //<-- As indicated by @Thomas Matthews you don't actually need the third check here
            c = b;
        }
        cout << "Greater value is " << c;
    }
} 

Other ways to write this:

if (a == b){
    cout << "Both are equal";
}else if (a > b) {
    cout << "Greater value is " << a;
} else {        
    cout << "Greater value is " << b;
}

OR (less obvious):

int t = a - b;
switch ((t > 0) - (t < 0)){ //<-- effectively computing the sign of t
    case -1: cout << "Greater value is " << b; break;
    case 0: cout << "Both are equal"; break;
    case 1: cout << "Greater value is " << a; break;
}
MikeMB
  • 20,029
  • 9
  • 57
  • 102
  • I wasn't aware python and c++ were so similar with if statements, do you know any more efficient way of doing this? – print'Words' Apr 06 '15 at 19:39
  • @print'Words': In terms of run time efficency or fewer lines of code? There are a few alternate ways how to wirte it, but I doubt any of them has any significant impact on the performance. – MikeMB Apr 06 '15 at 20:05
  • @print'Words': come to think of it: You can do it with fewer lines of code (see edit), but not really with less code . – MikeMB Apr 06 '15 at 20:23
0

Here is a simple fragment:

if (a == b)
{
  cout << "a == b";
}
else
{
  if (a < b)
  {
    cout << "a < b";
  }
  else
  {
    cout << "a >= b";
  }
}
cout << "\n";

You only need 2 comparisons.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154