0
#include <iostream>
using namespace std;

int main()
{
    int choose, num1, num2, result1, result2;
    cout << "Type '1' for plus, '2' for minus\n";
    cin >> choose;
    if (choose = 1) {
        cout << "Enter the first number :\n";
        cin >> num1;
        cout << "Enter the second number :\n";
        cin >> num2;
        cout << "Here's the result :\n";
        result1 = num1 + num2;
        cout << result1;
    }
    if (choose = 2) {
        cout << "Enter the first number :\n";
        cin >> num1;
        cout << "Enter the second number :\n";
        cin >> num2;
        cout << "Here's the result :\n";
        result2 = num1 + num2;
        cout << result2;
    }
    while (choose != 1 || 2) {
        cout << "You need to type '1' or '2'!\n";
        cin >> choose;
        if (choose = 1) {
            cout << "Enter the first number :\n";
            cin >> num1;
            cout << "Enter the second number :\n";
            cin >> num2;
            cout << "Here's the result :\n";
            result1 = num1 + num2;
            cout << result1;
        }
        if (choose = 2) {
            cout << "Enter the first number :\n";
            cin >> num1;
            cout << "Enter the second number :\n";
            cin >> num2;
            cout << "Here's the result :\n";
            result1 = num1 + num2;
            cout << result2;
        }
    }
}

so i'm trying to create a simple calculator by typing 1 for "+" or 2 for "-" but doesn't seems to work, i use "while" so when you type other value it will ask you to type 1 or 2, but when i debug it, it always executed (the "while") even if i type 1 or 2, please help me.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
Zhxck
  • 7
  • 3

2 Answers2

-1

Your while condition (choose != 1 || 2) is equivalent to ((choose != 1) || 2). Which evaluates to true because an int that is different than 0 is true true when casted to bool in c++. You need to change your condition to while (choose != 1 && choose != 2). Here is a good answer that explains the int to bool: https://stackoverflow.com/a/31551979/4179302

However, that's not the only problem in your code. You should change your if statements from if (choose = 1) to if (choose == 1) because = is assigning and == is comparing.

Moreover, you can youse if and else if so if the first is met, the second is not checked.

apalomer
  • 1,895
  • 14
  • 36
  • 1
    +rep very helpful, anyway i learn from SoloLearn and they doesn't explain about these things, thank you so much. – Zhxck Jul 03 '20 at 08:05
  • 1
    `choose != 1 || choose != 2` is always true, really you need to change the code to `while (choose != 1 && choose != 2)` – john Jul 03 '20 at 08:24
  • OMG!! You are so much right!! I've changed the condition. – apalomer Jul 03 '20 at 08:36
-2

Try this (choose!=1)||(choose!=2) in place of "choose!=1 || 2 ". The condition inside the if condition, choose=1 should be replaced with choose==1. choose=1 is an assignment whereas choose==1 is comparison.

Slade
  • 1