Simple Calculator Problem (C++)

  • C/C++
  • Thread starter Vorde
  • Start date
  • Tags
    Calculator
In summary: 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;}Here you can see that the same code can be written more concisely using a switch/case, as well as taking advantage of C++'s ability to perform floating point operations.In summary, Vorde found that he was not able to enter integers into the calculator and ended up creating a switch statement to handle the different cases. He found
  • #1
Vorde
788
0
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)
 
Technology news on Phys.org
  • #2
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.
 
  • #3
Ah! Thank you so much! Switching it to a char command fixed the whole thing.

I am indebted.
 
  • #4
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.
 
  • #5
I don't know this shortcut yet. Seems super useful though, does it do exactly what it seems like it's doing?
 
  • #6
Vorde said:
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".
 
  • #7
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).
 
  • #8
I think I understand and it definitely seems to be shorthand, but it seems to me that using the more general 'if' clause let's 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.
 
  • #9
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.
 

1. What is a simple calculator problem in C++?

A simple calculator problem in C++ is a program that allows users to perform basic mathematical calculations such as addition, subtraction, multiplication, and division using input values provided by the user.

2. How do I create a simple calculator in C++?

To create a simple calculator in C++, you will need to first declare and initialize variables for the input values and the result. Then, use arithmetic operators and control structures such as if/else statements or switch cases to perform the desired calculation. Finally, print the result to the user.

3. Can I add more complex functions to a simple calculator in C++?

Yes, you can add more complex functions to a simple calculator in C++. This can be done by using pre-defined functions from the cmath library or by creating your own custom functions to perform advanced calculations.

4. How do I handle errors in a simple calculator program in C++?

You can handle errors in a simple calculator program in C++ by implementing error handling techniques such as try/catch blocks or using conditional statements to check for valid input from the user. This will help prevent the program from crashing or producing incorrect results.

5. Is it possible to create a simple calculator with a graphical user interface in C++?

Yes, it is possible to create a simple calculator with a graphical user interface in C++. This can be achieved by using libraries such as Qt or wxWidgets to design and implement a user-friendly interface for the calculator program.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
3
Views
701
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top