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:

## All 5 Replies

To test for equality use the == operator = is the assignment operator.

Another thing to note is that your `if`s are all indepenedent (i.e. they're not `else if`s). 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.

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.

Be a part of the DaniWeb community

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