mercredi 13 février 2019

if...else if statements aren't working correctly? [on hold]

I wrote a simple code, but the if statements aren't working correctly

when I set something in the first if statement, example; if(option = 1) and the I write else if(option = 2) below it, it always executes the first, it doesn't matter whether the option is set to 1 or not.

  int num1, num2;
  int option;
  cout << "1) Addition" << endl;
  cout << "2) Subtraction" << endl;
  cout << "3) Multiplication" << endl;
  cout << "4) Division" << endl;
  cout << "Choose an action by writing the number next to it: " << endl;
  cin >> option;

  if(option = 1){
    cout << "You chose addition! " << endl;
    cout << "Choose two numbers, then write them down: " << endl;
    cout << "first number: ";
    cin >> num1;
    cout << "second number: " << endl;
    cin >> num2;
    cout << "The answer is: " << num1 + num2 << endl;
  }else if(option = 2){
    cout << "You chose subtraction! " << endl;
    cout << "Choose two numbers, then write them down: " << endl;
    cout << "first number: ";
    cin >> num1;
    cout << "Second number: ";
    cin >> num2;
    cout << "The answer is: " << num1 - num2 << endl;
  }else if(option = 3){
    cout << "You chose multiplication! " << endl;
       cout << "Choose two numbers, then write them down: " << endl;
       cout << "first number: ";
       cin >> num1;
      cout << "Second number: ";
       cin >> num2;
       cout << "The answer is: " << num1 * num2 << endl;
    }else if(option = 4){
      cout << "You chose division! " << endl;
      cout << "Choose two numbers, then write them down: " << endl;
     cout << "first number: ";
     cin >> num1;
     cout << "Second number: ";
      cin >> num2;
     cout << "The answer is: " << num1 / num2 << endl;
   }else {
     cout << "Error 505" << endl;
   }    

Aucun commentaire:

Enregistrer un commentaire