New Reply

Simple Calculator Problem (C++)

 
Share Thread
Aug19-12, 10:51 AM   #1
 
Recognitions:
Gold Membership Gold Member

Simple Calculator Problem (C++)


Hello all.

I'm teaching myself C++ (my first programming language), and as an exploration I decided to see if I could make a simple four-function calculator. The thing is I'm having trouble. I managed to successfully write a program that would input two integers and return all four functions of the two (their sum, difference, quotient and product) but I couldn't get it to just give me one at a time (a problem with my if clauses I think).

So I started from scratch and wrote this:

Now I get a return value of 0, regardless of the input integers and the chosen function. I'm new to C++, so could anyone tell me what I'm doing wrong?

Thank you.

Code:
int chooseOperator(int OP, int num1, int num2)
{

	if (OP == '+')
		return add(num1, num2);
	if (OP == '-')
		return subtract(num1, num2);
	if (OP == '*')
		return multiply(num1, num2);
	if (OP == '/')
		return divide(num1, num2);
	else 
		return 0;
}



int main()
{
	using namespace std;
cout << "Thank you, will you please enter your first integer:" << endl;
		int num1;
			cin >> num1;
cout << "Thank you, will you please enter your second integer:" << endl;
		int num2;
			cin >> num2;
cout << "Will you please choose an operator:" << endl;
		int O;
			cin >> O;
cout << chooseOperator(O, num1, num2);

		cin.clear();
		cin.ignore(255, '\n');
		cin.get();

return 0;
}
(I didn't copy and paste the trivial stuff like the function definitions but I'm not worried that's the problem)
PhysOrg.com science news on PhysOrg.com

>> City-life changes blackbird personalities, study shows
>> Origins of 'The Hoff' crab revealed (w/ Video)
>> Older males make better fathers: Mature male beetles work harder, care less about female infidelity
Aug19-12, 11:09 AM   #2
 
Recognitions:
Homework Helper Homework Help
Hi Vorde!

You have:
Code:
int O;
cin >> O;
This will read an 'integer' from standard input.
That is, spaces and new lines are skipped, and digits are expected.

When you enter for instance '+', that can not be converted to an integer.
This would leave your input stream in an invalid state, and your integer in an uninitialized state.

It seems you've already found that the stream state is invalid, since you added cin.clear() afterward.
Aug19-12, 11:51 AM   #3
 
Recognitions:
Gold Membership Gold Member
Ah! Thank you so much! Switching it to a char command fixed the whole thing.

I am indebted.
Aug19-12, 04:01 PM   #4
 
Admin

Simple Calculator Problem (C++)


Now that you got this part right, change your if statements to a switch/case:

Code:
switch(OP)
{
	case '+': return add(num1, num2);
	case '-': return subtract(num1, num2);
	case '*': return return multiply(num1, num2);
	case '/': return divide(num1, num2);
	default: return 0;
}
Note: in a way this is a shortcut - each case should be ended with a break. But it doesn't matter in this particular situation, as return guarantees code execution stops.
Aug19-12, 05:43 PM   #5
 
Recognitions:
Gold Membership Gold Member
I don't know this shortcut yet. Seems super useful though, does it do exactly what it seems like it's doing?
Aug19-12, 06:16 PM   #6
 
Recognitions:
Science Advisor Science Advisor
Quote by Vorde View Post
does it do exactly what it seems like it's doing?
No, not quite.

Most languages have something like a switch/case type control structure that allows one individual case to be executed out of a range of choices. C/C++ is a bit unusual however, in that the default behavior is to "drop through" (and hence execute all subsequent cases) rather than just execute the selected case. To overcome this you need to add a "break" command at the end of each case (that is unless you actually want it to "drop through" of course).

Boreks point was that in this particular instance, the fact that each case clause consists of a "return" means that you don't have to worry about "drop through".
Aug20-12, 02:03 AM   #7
 
Admin
Compare:

Code:
switch(OP)
{
	case '+': return add(num1, num2);
	case '-': return subtract(num1, num2);
	case '*': return return multiply(num1, num2);
	case '/': return divide(num1, num2);
	default: return 0;
}
and

Code:
switch(OP)
{
	case '+': answ = add(num1, num2);
		  break;
	case '-': answ = subtract(num1, num2);
		  break;
	case '*': answ = multiply(num1, num2);
		  break;
	case '/': answ = divide(num1, num2);
		  break;
	default: answ = 0;
}
return answ;
They both do the same - one stops the execution leaving the function (which is a shortcut I was referring to), the other by using break commands (which stops execution of switch and jumps to the end of the list as expected). Note that in the latter case the break command is not needed after the last case entry (but putting it there is not an error, just doesn't make much sense).
Aug20-12, 02:08 AM   #8
 
Recognitions:
Gold Membership Gold Member
I think I understand and it definitely seems to be shorthand, but it seems to me that using the more general 'if' clause lets you be more versatile with your conditions. I was making some changes (adding a decimal-to-binary function) and it was useful to be able to change the requirements of one of the if statements (adding a &&) but not the others, whereas I don't think what I did would be as easy using the 'case' system.
Aug20-12, 02:37 AM   #9
 
Admin
Both if and case/switch are valid tools, sometimes it is better to use one, sometimes it is better to use the other. I believe switch/case is faster when there are many values, for sure code is more readable. Note you can always do something like

Code:
	case '/': if (num2 != 0) answ = num1/num2;
			else answ = MAXINT;
		  break;
if you want to add a logical condition.
New Reply

Similar discussions for: Simple Calculator Problem (C++)
Thread Forum Replies
How does a simple calculator work? General Engineering 10
Calculator problem Engineering, Comp Sci, & Technology Homework 0
Is my calculator giving the wrong score or...? (Simple calculation question) Precalculus Mathematics Homework 11
Problem with TI-84 Calculator Calculators 3
Casio FX 9750GPlus-Graphing-Calculator- How do you do simple operations/navigate? Calculators 0