Need help with the correct use of "IF"

Hi guys, I'm pretty new and a I would like to ask 2 little things about my first program ever.

So I wrote this simple code, and the 2 things I would like to ask are:

1) Why "if" is not actually checking the result?! I mean, even if the result1 is not 0 it keeps writing "If the result was 0 then C was 0"

2) Can I write the 4 "cout" in lines 20-23 in one single cout? Cause when I tried it gave me error.

Bonus question: Other consideration about the code and how to make it more "compact" are appreciated too.

Thanks in advance, coding is a f****** blast!

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
  #include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
  int a, b, c, result1, result2;
  printf ("Write a number (a): ");
  scanf("%d", &a);

  printf("Write another number (b): ");
  scanf("%d", &b);

  printf("Another one (c): ");
  scanf("%d", &c);

  result1 = a+b-(c+a+b);
  result2 = a+b+c;

  cout << "The result is: " ;
  cout << result1;
  cout << " and: " ;
  cout << result2 << endl;


  if (result1 = 0);
  {
      cout << "If the result was 0 then C was 0";
  }

  return 0;
}
Last edited on
1) The compiler says this:
 In function 'int main()':
26:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]


2) Yes. Show what you did and the error.


Bonus:
* Why printf, scanf and std::cout?
* a+b-(c+a+b) == a+b-c-a-b == -c
Thanks,
I can't understand what's the connection between parantheses at int main with "if" condition not being checked...

With cout, I tried for example merge the first 2 like this : cout << "The result is": result1 ;
And din't work.

I used printf, scanf and std because i didnt know what to use otherwise... And about the expression resulting in -c well i wasn't paying attention, I just tried something to add "if" and see if i understood how to use it.

Edit: I changed if (result1 = 0); in if ((result1 = 0)); and the warning tip disappeared but still the condition is not getting checked.
Last edited on
Hi,

= is for assignment, whereas == is for comparison - which is what you want for a condition expression. And remove the semicolon on line 26

The following is probably not the worlds best advice (it has it's limitations), write the if statement like this:

1
2
3
if (  0 == result1) {

}


That way, if you forget to use == , the compiler will complain. But hopefully having made this mistake, you will now remember to always use == for comparison.

For std::cout :

std::cout << "The result is " << result1 << "\n";

Try to use std::cout and std::cin rather than scanf and printf.

Good Luck !!
Thanks again, but why is better use cin and cout instead of scanf/printf ?
It's very easy to introduce bugs when using scanf and printf.

With printf() and scanf(), it's up to you to ensure that the format strings match the types of the arguments. For example
1
2
short x;
scanf("%d", &x);  // bug 

Here you're telling scanf to scan an integer, but the address you provide points to a short so the behavior is undefined. In contrast:
1
2
short x;
cin >> x;

This is always safe because the compiler knows that you're scanning a short.
Thanks to all who helped, marking the topic as solved.
Topic archived. No new replies allowed.