mercredi 2 août 2017

Messed up if/else statement for calculator

I tried to make a basic calculator using C++ if/else. When running though, regardless of what is input, the output in always only addition. I'm assuming my if/else statements are not done correctly but I can't figure it out. Thanks.

#include <iostream>
using namespace std;

int main()
{
int a;
int b;
int c;

cout << "Enter the number corresponding to the wanted operation.\n\n" << "1. Addition\n" << "2. Subtraction\n" << "3. Multiplication\n" << "4. Division\n\n";
cin >> a;
cout << "\nGood job. Now enter the first number you want used in the operation.\n";
cin >> b;
cout << "Okay. Now enter the second number.\n";
cin >> c;
cout << "\n";

if (a = 1) {
    cout << b << " Plus " << c << " is " << b + c << "\n";
}
    else if (a = 2) {
    cout << b << " Minus " << c << " is " << b - c << "\n";
}
    else if (a = 3) {
    cout << b << " Times " << c << " is " << b * c << "\n";
}
    else if (a = 4){
        cout << b << " Divided by " << c << " is " << b / c << "\n";
}
else {
    cout << "An error occurred.";
}
system("pause");

}

Aucun commentaire:

Enregistrer un commentaire