I was given a question from my college's e-learning blackboard:

Write a program that will act as a simple calculator. It should ask the user for a floating point number, an operator, and another floating point number. The program should first check the operator is either ‘+’, ‘-‘, or ‘’. If it is, then it should output the result of performing the operation on the two floating point numbers. If the operator is not a ‘+’, ‘-‘, or ‘’ the program should print the message “Illegal operation!”

Sample input and output:
Enter a number: 10
Enter an operator (+, - or *): +
Enter another number: 30
10 + 30 = 40

Show your solution in three version of decision logic:
(1)Straight-through Logic. Save project file as calculator-STL.
(2)Positive Logic. Save project file as calculator-PL.
(3)Negative Logic. Save project file as calculator-NL.

My code is the straight-through logic. See below:

// calculator.cpp : C++ calculator.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    float result; // The result of the equation.
    char operator1; // Math operator.

    float num1, num2; // Two numbers for the equation.

    //input

    cout<<"Enter a number         : ";
    cin>>num1;

    cout<<"Enter an operator (+, - or *)  : ";
    cin>>operator1;

    cout<<"Enter another number       : ";
    cin>>num2;

    //process

    if (operator1 = '+')
    {
        result = num1 + num2;
    }

    if (operator1 = '-')
    {
        result = num1 - num2;
    }

    if (operator1 = '*')
    {
        result = num1 * num2;
    }

    else
    {
        cout<<"Illegal operation!"<<endl;
    }

    //output

    cout<< num1 << operator1 << num2 <<'='<< result <<endl;

    system("pause");
    return 0;
}

The result in the console application gave me like this:

fe1e94b7471864a475e1ee84e41fe31f

Another thing to note is that your ifs are all indepenedent (i.e. they're not else ifs). So the else only applies to the last if. That means that once you've fixed the = vs. == issue, the else will execute whenever you entered an operator other than *.

Anothernother thing to note is that you print the result of the calculation even if the else executed. In that case result would be uninitialized invoking undefined behavior (most likely printing a garbage value to the screen).

Also, besides the == operator as mentioned by ddanbe, you want to use conditional logic, or a switch statement. IE,

// Conditional logic
    if (operator1 == '+')
    {
        result = num1 + num2;
    }
    else if (operator1 == '-')
    {
        result = num1 - num2;
    }
    else if (operator1 == '*')
    {
        result = num1 * num2;
    }
    else
    {
        cout<<"Illegal operation!"<<endl;
    }

Or:

// Using switch statement
    switch (operator1)
    {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        default:
            cout<<"Illegal operation!"<<endl;
            break;
    }

rubberman, your conditional logic code has breaking problems when entering an illegal operator.

a9f44c7d1ca892efba8852d2022d0bf4

And the switch statement, I haven't learned before in class.

rubberman, your conditional logic code has breaking problems when entering an illegal operator.

So does yours (after fixing the assignment issue). As I pointed out earlier, you need to make it so that the result only gets printed, if a legal operator has been entered.

This article has been dead for over six months. Start a new discussion instead.