Problem with the if statment

Okay so I learned the if statment today and tried combining it with a calculator. And I thouth if i wrote
1
2
3
  if(ans = 100) {
    cout << "You are awesome! \n \n";
   } 

it will print out You are awesome! everytime your answer is 100. But it is not working everytime I enter something like 10 and 50 the answer will be 60 but it still prints out you are awesome. Why is that?

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
  #include <iostream>

using namespace std;

int main()
{

  int a;
  int b;
  int ans;

  cout << "Enter a number and press ENTER! \n";
  cin >> a;

  cout << "Enter another number and press ENTER! \n";
  cin >> b;

  ans = a + b;
  cout << "The answer is ";
  cout << ans;
   cout << "\n";

   if(ans = 100) {
    cout << "You are awesome! \n \n";
   }

    return 0;
}
Try this :

if(ans == 100)

Equality operator, not assignment.

HTH
Topic archived. No new replies allowed.