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

Dani AI

Generated

— the behavior described follows from a common C/C++ pitfall that and correctly pointed toward: one of the condition expressions always evaluates as true, so the add-branch runs every time. There are a few other issues in the posted code worth fixing while correcting that comparison: a missing pair of braces in the subtraction branch (which makes a cout run outside the intended else), integer division truncation for the divide case, and no check for divide-by-zero.

Always compare the operation char explicitly (or normalize it with toupper and check only uppercase). Always use braces for if/else blocks, even for single statements, to avoid unintended execution. For division, prefer double (or cast operands) to avoid truncation and explicitly handle 0 as the divisor.

A compact, robust pattern replaces chained if checks with a switch on the normalized operation and demonstrates safe division and clear flow:

#include <iostream>
#include <cctype>

int main() {
    char op;
    double a=0.0, b=0.0;
    std::cout << "Enter A/S/M/D then two numbers: ";
    if (!(std::cin >> op >> a >> b)) return 0;
    op = std::toupper(static_cast<unsigned char>(op));

    switch (op) {
        case 'A':
            std::cout << "Sum: " << (a + b) << '\n';
            break;
        case 'S':
            std::cout << "Difference: " << (a - b) << '\n';
            break;
        case 'M':
            std::cout << "Product: " << (a * b) << '\n';
            break;
        case 'D':
            if (b == 0.0) std::cout << "Error: divide by zero\n";
            else std::cout << "Quotient: " << (a / b) << '\n';
            break;
        default:
            std::cout << "Unknown operation\n";
    }
    return 0;
}

Notes: std::toupper should be called with unsigned char cast to avoid undefined behavior on some platforms. Remove Windows-only system("pause") for portability. Following these changes will fix the always-add behavior and make the program safer and clearer.

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.