Any ideas on how to do this program because I have the slightest idea. This is what I have to do, Multi-Function Calculator - Ask the user for 2 numbers, then ask them to select from a numbered list of operations. As rudimentry error checking, give them an extra chance if they enter an invalid value. Depending on the operator selected (+, -, *, and /) do the operation on the numbers and print out the result including the equation with the proper operator. Remember to check for situations such as division by zero. You'll have to use floating-point numbers here as well.

## All 5 Replies

Don't get too overwhelmed -- this is just a simple console program. Begin with the simplest program you can think of then gradually add more code to make it do what the requirements state. Don't attempt to implement everything all at one time. Here are a couple hints:

select from a numbered list of operations
This is a menu to select what operation to perform

``````1.  Add
2.  Subtract
3.  Divide
4.  Multiply``````

Then get user input to select one of the above menu items. Then code a switch statement to perform the desired operation

``````switch(Selection)
{
case '1':
break;
// etc. etc
}``````

I was just going to use the multiplication operation. How would I go about that using the switch control structure? Also, my professor would like my answer to be in decimal form, and I was reading about how to do that in my text, but I'm a little confused.

you will want to use floats --

``````float value1, value2, answer;

value1 = 1
value2 = 2
// divide operator
//
// multipication operator

// etc``````

as for the switch -- I already posted a brief example. your text book probably has a more thorough explaination.

You might want to use doubles instead of floats for greater accuracy.

Multi-Function Calculator - Ask the user for 2 numbers, then ask them to select from a numbered list of operations.

You can do this with cin; use first two doubles and then a char.

As rudimentry error checking, give them an extra chance if they enter an invalid value.

Put the cin>>ch in a loop, and only break out of the loop when a valid operator was entered.

Depending on the operator selected (+, -, *, and /) do the operation on the numbers and print out the result including the equation with the proper operator.

Use a switch statement, as Ancient Dragon indicated, like this:

``````switch(ch) {
case '+':
// ...
break;
case '-':
// ...
}``````

Remember to check for situations such as division by zero.

In your case '/':, put an if(number2==0).

You'll have to use floating-point numbers here as well.

doubles (or floats) take care of this.

I am not very good programmer. But i just want to help you with my ideas. C++ contains operator overloading. You can refer the operator overloading methods. Or simple way u can also try function which adds , substracts and performs all the operation.

First ask for input on the console panel and pass that inputs into the function separate the operand and the operator from each other check all the validations and conditions and then perform operation.