### Calculator help!

Hey guys, I need help with a calculator. I made one for class but the one I made was not what my professor wanted and I don't know how to get exactly what he wants. He wants me to create a mini calculator program that will allow the user to enter two integers (operand 1 and operand 2), the program will calculate the sum, difference, product, whole number quotient, remainder, the real number quotient, and the square root of operand1. The program should also calculate the value of operand2 raised to the power of 4.

This is what I made so far, how can I fix it and make it into what he wants. He wants me to input 2 operands and have them do all the functions at once, can I get a helping hand anyone?

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 #include #include #include int main () { using namespace std; int selection (0); double input1(0.0); double input2(0.0); char indicator('n'); cout << "Welcome to Andrew's C++ Calculator" << endl; for (;;) { cout << endl << "Please chose your function: " << endl << "1 for Addition" << endl << "2 for Subtraction" << endl << "3 for Multiplication" << endl << "4 for Division" << endl << "5 for Square Root" << endl << "6 for Power" << endl << "0 to exit the program" << endl << endl << "Enter your selection number: "; cin >> selection; switch (selection) { case 1: cout << endl << "Enter the first number to Add: "; cin >> input1; cout << endl << "Enter your second number: "; cin >> input2; cout << endl << "The sume of " << input1 << " and " << input2 << " is " << input1 + input2 << endl; break; case 2: cout << endl << "Enter the first number to Subtract: "; cin >> input1; cout << endl << "Enter your second number: "; cin >> input2; cout << endl << "The difference of " << input1 << " and " << input2 << " is " << input1 - input2 << endl; break; case 3: cout << endl << "Enter the first number to Multiply: "; cin >> input1; cout << endl << "Enter your second number: "; cin >> input2; cout << endl << "The product of " << input1 << " and " << input2 << " is " << input1 * input2 << endl; break; case 4: cout << endl << "Enter the first number to Divide: "; cin >> input1; cout << endl << "Enter your second number: "; cin >> input2; cout << endl << "The quotient of " << input1 << " and " << input2 << " is " << input1 / input2 << endl; break; case 5: cout << endl << "Enter the number you want the Square Root of: "; cin >> input1; cout << endl << "The Square Root of " << input1 << " is " << sqrt(input1) << endl; break; case 6: cout << endl << "Enter the Base number: "; cin >> input1; cout << endl << "Enter your exponent: "; cin >> input2; cout << endl << input1 << " to the power of " << input2 << " is equal to " << pow(input1, input2) << endl; break; case 0: exit(EXIT_SUCCESS); default: cout << endl << "The selection number, " << selection << ", you have entered is invalid." << endl; break; } cout << endl << "Would you like to preform another calculation? (y or n): "; cin >> indicator; if((indicator == 'n') || (indicator == 'N')); } return 0; }
Have you learned about sub programs yet? You could save a lot of typing by doing a subprogram for i/o like this

 1234567 void get_input( int &input1 , int &intput2 ) { cout << "Please enter operand one: "; cin >> input1; cout << "Please enter operand two: "; cin >> input2; }

Then call it like

 get_input( input1 , input 2 );

For those 6 times in your switch.

Also I am not exactly sure what your instructor wants. It sounds like you are trying to say that you need to output this stuff:

sum = operand1 + operand2
difference = operand1 + operand2
product = operand1 * operand2
integer quotient = static_cast<int>( operand1 / operand2 ); //you want to cast to an int
remainder = operand1 % operand2
rational quotient = operand1 / operand2
sqrt of operand 1 = sqrt( operand1 );
operand2 power of 4 = pow( operand2 );

Could you maybe post the assignment?
Wow, you just made things 100% easier. I did just figure out how to simplify it a bit but it does look a bit choppy and messy.

I also do not want to post the full assignment because I do want to try to figure it out as much as I can by myself. But you did help make things simpler, thanks a bunch.
Last edited on
Topic archived. No new replies allowed.