Ok, The problem is that when I execute this program (C++) what ever numbers I put into the answer slot it is given me the answer to the numbers of what ever I choose whether it would be Add, Subtract, Multiply or Divide as a Sum and not Difference or Product or Quotient. And it is not working as what I am asking it to do. Like if I pick Division it is adding the two numbers or if I pick Multiply it is still adding the two numbers.
Not sure what is going on.

//This program will add, subtract, multiply or divide depending on what the user inputs

#include <iostream>

using namespace std;

int main()
{
    char operation = ' ';
    int num1 = 0;
    int num2 = 0;
    int answer = 0;

    cout <<"Enter A (add) or S (subtract) or M (multiply) or D (divide): ";
    cin >> operation;
    cout <<"Enter first number: ";
    cin >> num1;
    cout <<"Enter second number: ";
    cin >> num2;
    operation = toupper(operation);

    if (operation != 'A' && operation != 'S' && operation != 'M' && operation != 'D')
//Changed this to && instead of ||
    {
        cout <<"Error: Wrong letter entered" << endl;
        return 0;
    }

    if (operation == 'A' || 'a')
    {
        answer = num1 + num2;
        cout << "Sum: " << answer << endl;;

    }

    else if (operation == 'S' || 's') 
    {
            if (num1 > num2)
            {
                answer = num1 - num2;
                cout << "Difference: " << answer << endl;
            }
            else 
                answer = num2 - num1;
                cout << "Difference: " << answer << endl;
    }


    else if (operation == 'M' || 'm')
    {
        answer = num1 * num2;
        cout <<"Product: " << answer << endl;
    }

    else if (operation == 'D' || 'd')
    {    
        if (num1 > num2)
        {
            answer = num1 / num2;
        }
        else
            answer = num2 / num1;

        cout <<"Quotient: " << answer << endl;
    }

    system("pause");
    return 0;
}//end of main function

Recommended Answers

All 2 Replies

To start with: Welcome to Daniweb.

Please use code tags when posting code to maintain the indentation you (hopefully) use when writing your code. It is a bit of nuisance to do, but once you've done it it isn't difficult, and many of the permiere responders won't read unformatted code so you are selling yourself short if you don't do it.


if (operation == 'A' || 'a')

That syntax is wrong, unless the new standards and your compiler allow it (but I don't think that's the case). You need to do individual comparisons like you did earlier in your code.

if (operation == 'A' || 'a')

as what Lerner said,
and by the way it should be written as

if (operation == 'A' || operation == 'a')

other than this I think it should work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.