1

I'm extremely new to C++, I was following a tutorial and wanted to go a bit off what the course said, I attempted to make a basic calculator that instead of just being able to add could subtract, divide and multiply but it still seems to only be able to add, why is this?

  int num1, num2;
  double sum;
  int addType;
  cout << "Type a number: ";
  cin >> num1;
  cout << "Type a second number: ";
  cin >> num2;
  cout << "Do you want to 1. add, 2. subtract, 3. divide or 4. multiply: ";
  cin >> addType;
  if (int addType = 1) {
    sum = num1 + num2;
  }
  else if (int addType = 2) {
    sum = num1 - num2;
  }

  else if (int addType = 3) {
    sum = num1 / num2;
  }
  else if (int addType = 4) {
    sum = num1 * num2;
  }
  
  cout << "Your total is: " << sum;
  
}
  • 8
    `int addType = x` --> `addType == x`. Get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of improvising from tutorials. – molbdnilo Jun 21 '22 at 14:07
  • 1
    `if (int addType = 1) {` creates a new variable `addType` which shadows and sets its value to 1. Then tests if it is not zero and executes the `sum = num1 - num2;` because 1 is not 0. – drescherjm Jun 21 '22 at 14:09
  • 1
    If you can't get your hands on a book, try working through these tutorials first : https://www.learncpp.com/ (books can be outdated, or focus more on teaching about datastructures using C++ then teaching about good programming using C++ ) – Pepijn Kramer Jun 21 '22 at 14:11
  • don't ignore compiler warnings https://godbolt.org/z/K4TK5z96e. – 463035818_is_not_an_ai Jun 21 '22 at 14:20
  • *I was following a tutorial and wanted to go a bit off what the course said* -- Don't do this if you're a beginner in C++. If you want to go "off course", then the new course better be following another good C++ tutorial. As you can see, you can write C++ that has valid syntax, but is still junk and does something totally different than what you expect. – PaulMcKenzie Jun 21 '22 at 14:22
  • i'm of the opinion doing anything is acceptable. Write your own code or follow a tutorial is doing. Doing is good. Focus on learning and enjoying now. You can worry about mastery later. Best of luck to you on your endeavors @Aviation6! – netskink Jun 21 '22 at 14:38

4 Answers4

3

You are creating new variable in if condition part, update condition part to check if it is equal to something with addType == x

int num1, num2;
 double sum;
 int addType;
 cout << "Type a number: ";
 cin >> num1;
 cout << "Type a second number: ";
 cin >> num2;
 cout << "Do you want to 1. add, 2. subtract, 3. divide or 4. multiply: ";
 cin >> addType;
 if (addType == 1) {
   sum = num1 + num2;
 }
 else if (addType == 2) {
   sum = num1 - num2;
 }

 else if (addType == 3) {
   sum = num1 / num2;
 }
 else if (addType == 4) {
   sum = num1 * num2;
 }
 
 cout << "Your total is: " << sum;
 
}

Elvis Oric
  • 1,289
  • 8
  • 22
  • So == is standard formatting, cheers – CriticalBackend Jun 21 '22 at 14:12
  • Not sure what you mean by "standard formatting". A single `=` _assigns_ the value on the right hand side (rhs) to the variable on the left hand side (lhs): `sum = num1 + num2`. A double `==` _compares_ the rhs and the lhs: `if(num2 == 0) { ... }`. They are two completely different operators (that happen to look similar) regardless of "formatting". – CompuChip Jun 21 '22 at 14:45
1

Instead of checking equality, your if conditions are initialising the addType var. It should be:

if (addType == 1)

= means to assign a value, which will set addType to the value of 1, and then the if will always be true.

Rogue
  • 11,105
  • 5
  • 45
  • 71
Aria_Z
  • 13
  • 3
1
if (int addType = 1)

means assign 1 to "addType", "addType" is alway 1, so condition alway is true.You will only ever be able to add.

Yeonon
  • 21
  • 3
0
if(int addType = 1) 

this is a expression which defines a variable named addType and assigns it's value to 1. so it basically becomes

if(1)

which is true. so instead of this write:

if( addType ==1)
Zagatho
  • 523
  • 1
  • 6
  • 22