Simple Calculator Not Working

I'm new to c++ and I don't know very much. I was trying to make a simple calculator but the if statements don't seem to work. It will run both of them no matter what I type. I can't seem to figure it out. Sorry for me being so dumb. Could anyone help me out?

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
#include <iostream>
#include <string>

using namespace std;

int addition(int num1, int num2){
    int answer = num1 +  num2;
}

int subtraction(int num1, int num2){
    int answer = num1 - num2;
}

int main()
{
    string aOS;
    cout << "Would you like to add or subtract? (Enter Add or Subtract)" << endl;
    cin >> aOS;
    if(aOS == "Add" || "add"){
        int addA;
        int a;
        int b;
        cout << "Enter the first number: " << endl;
        cin >> a;
        cout << "Enter the second number: " << endl;
        cin >> b;
        addA = addition(a, b);
        cout << endl << "The answer is: " << addA << endl;
    }
    if(aOS == "Subtract" || "subtract"){
        int subA;
        int a;
        int b;
        cout << "Enter the first number: " << endl;
        cin >> a;
        cout << "Enter the second number: " << endl;
        cin >> b;
        subA = subtraction(a, b);
        cout << endl << "The answer is: " << subA << endl;
    }
    return 0;
}
You need to explicitly check || aOS == "add", not just || "add". Similar change for the subtraction.
Last edited on
Thanks. It works now.
Hmmm....I'm not sure if you can use this, but I made a quick code that asks the user if they want to add or subtract then prompts them to input 2 numbers then either adds or subtracts depending on their choice.
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	double num1, num2, add, sub;
	string choice;

	cout << "Do you want to add or subtract?\n";
	cin >> choice;
	if (choice == 'add' || choice == 'Add' || choice == 'ADD')
	{
		cout << "Please enter the first number.\n";
		cin >> num1;
		cout << "Please enter the second number.\n";
		cin >> num2;
		add = (num1 + num2);
		cout >> "The sum of " << num1 << " and " << num2 << "is " << add << ".";
	}
	if (choice == 'subtract' || choice == 'Subtract' || choice == 'SUBTRACT')
		cout << "Please enter the first number.\n";
		cin >> num1;
		cout << "Please enter the second number.\n";
		cin >> num2;
		sub = (num1 - num2);
		cout >> "The difference of " << num1 << " and " << num2 << "is " << sub << ".";
	return 0;
}


I'm not sure if itll run correctly. I made this in like 3 mins.
Just a thought, I don't know if the assignment requires they type in a word, but you could have them enter a menu choice (e.g 1 or 2) instead of making them spell out an actual word and trying to account for different spelling/capitalization.
Topic archived. No new replies allowed.