If statement

Hello guys, I wrote this simple program but I keep getting wrong outputs and I don't know what is the problem.
The program needs to perform some calculations under some conditions
If b<c it Should perform (b-a)+(c-b)
If a=b it should perform (c-b)
If a=c it should perform (a-b)
If b=c it should perform (b-a)
And if a<b it should perform (b-a)+(c-b)

Please help if you have any ideas.



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
#include <iostream>
using namespace std;
 int main()
 {
   int a,b,c,d,e,f,g,h;
   cout <<"please enter three numbers : ";
   cin >> a>> b >> c;
   
     d=(b-a)+(c-b);
     e=c-b;
     f=a-b;
     g=b-a;
     h=(b-a)+(c-b);
  
  if(b<c)
  cout << d;
  if(a=b);
  cout << e;
  if(a=c);
  cout << f;
   if(b=c);
  cout << g;
   if(a<b);
  cout << h;
     
      return 0;
 }
Last edited on
PLEASE USE CODE TAGS.

PLEASE TELL US WHAT THE PROGRAM IS SUPPOSED TO DO ... SO THAT WE CAN SEE WHAT IT ISN'T DOING.

Then ...
you have at least three = where you mean ==.
And ...
a load of if statements are one line long, because you have put some unlikely semicolons (;) immediately after the test.

You do know that variable names are allowed to be more than one letter, don't you? And that you are allowed to use the results of previous calculations?
Last edited on
Ok let me edit it
OK, so you've edited it to include some sort of instructions. We await the code tags.

In C++ (and most programming languages), = means assignment ("setting equal to") whereas == tests for equality ("is it the same value as?"). In your code (unlike your mathematics) you need ==. Thus ...
if(a=b)
should be
if ( a == b )

When that is corrected, the same line in your code has a second error.
if ( a == b ) ;
has a semicolon at the end ... which means that the thing you anticipate doing if the condition is true ... stops at the end of the line (and would essentially do nothing). Accordingly, cout << e; would be done regardless of the test. So you should have
1
2
3
4
if ( a == b )
{
   cout << c - b << '\n';
}

For a one-line action the curly braces aren't essential. However, they will reduce the chance of you making an error here. You could also write cout << e << '\n'; if e has been evaluated as c - b; however, if writing out its value is all you do with it then that isn't necessary.

Last edited on
Thanks for the help
Topic archived. No new replies allowed.