CVHansen CVHansen - 1 month ago 13
C++ Question

Why is my 4-function simple C++ calculator only returning the first function?

I've hunted for this answer across the web and have found similar assignments given to various students, but none seemed to have my problem. That said, I'm not able to get this simple 4-function C++ calculator to do anything EXCEPT addition. I'm assuming it's an issue with my If/Else statements, but my code looks like every example I've seen both in my textbook and online, and my compiler is returning 0 errors or warnings. Why I can make a switch function work perfectly but can't seem to grasp these damnable if/else functions, I don't know, but I do know that I have been struggling with this same simple program for 6 FREAKING hours (and am already an hour late turning it in, so yay for a max score of 50%)! Someone, for the love of all that is holy, help me!!

I am using Microsoft Visual Studio 2015, and this is my code:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
//declare variables
char function = ' ';
int num1 = 0;
int num2 = 0;
double answer = 0.0;

//enter the input variables
cout << "What function? (A,S,M,D): ";
cin >> function;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number : ";
cin >> num2;

//calculate and display the answer by function chosen
function = toupper(function);

if (function = 'A')
{
answer = num1 + num2;
cout << "The sum is " << answer << endl;
}
else
{
if (function = 'S')
{
answer = num1 - num2;
cout << "The difference is " << answer << endl;
}
else
{
if (function = 'M')
{
answer = num1 * num2;
cout << "The product is " << answer << endl;
}
else
{
if (function = 'D')
{
if (num1 > num2)
{
answer = num1 / num2;
cout << "The quotient is " << answer << endl;
}
else
{
answer = num2 / num1;
cout << "The quotient is " << answer << endl;
}
}
else
{
cout << "Invalid entry" << endl;
} //end if
} //end if
} //end if
} //end if

return 0;
} //end of main function

Answer

A statement like:

if (function = 'A')

first assigns 'A' to function, then uses that value for the conditional (which, unless you assign '\0', will be true).

That's why you're only getting the first function.

You should use equality comparison rather than assignment:

if (function == 'A')

The prevalence of this problem is what causes otherwise sane people to suggest reversing the order of the arguments, such as with:

if ('A' == function)

and this will indeed cause a compile time error if you accidentally use = rather than ==.

However, it has two problems:

  • it won't work unless one of the arguments is a constant (like 'A') since swapping two variables won't allow the compiler to catch the case where you've used = instead of ==; and
  • it's seriously butt-ugly :-)

In my opinion, it's just best to get into the habit of checking whether you want to compare or assign, and realising that, if your comparisons seem to be misbehaving, this is one of the first things you should look for.

Comments