Add and Sub program

HELP PLEASE[/small]!!!
Every time i run this program, using the operatorCode S it always adds it instead of subtracting. What is wrong with it?

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
34
35
36
37
38
39
40
41
42
43
44
45
  // Program AddSub reads in a letter (A or S) and two int values.
// If the letter is A, the two values are added.
// If the letter is S, the second is subtracted from the first.
// The answer is printed appropriately labeled.
#include <iostream>

using namespace std;

int main ()
{
  char  operatorCode;
  int  value1;
  int  value2;
  int  answer;

  cout << "Input a letter and two integer values." << endl;
  cout << "If the letter is an A, the values are added."  << endl;
  cout << "If the letter is an S,"
       << "the second value is subtracted from the first." << endl;
  cout << endl;
  cin >> operatorCode;
  cin >> value1  >> value2;
  cout << endl;

  if (operatorCode = 'A')
  {
	  answer = value1 + value2;
    cout << value1  << " plus " << value2
         << " is " << answer  << endl;
  }
  else
  {
	  if (operatorCode = 'S')
	  {
		  answer = value1 - value2;
		  cout << value1  << " minus " << value2
         << " is "  << answer  << endl;
	  }
  }

	system("PAUSE");
  return 0;
}

Hi @meva21,
1
2
if (operatorCode = 'A') //line 25
if (operatorCode = 'S') //line 33 

you are using the
= assignment operator
instead of == relational operator
"Equal to"
Topic archived. No new replies allowed.