Hi, I was assigned a problem where I have to build a calculator but I'm a little confused on how to get it done. Here's what I'm asked to do:
Build a calculator program to perform the following functions (to help you a little, the C++ code to print the user screen is included here):
cout << "**********************************" << endl;
cout << "* 1 - Add *" << endl;
cout << "* 2 - Subtract *" << endl;
cout << "* 3 - Multply *" << endl;
cout << "* 4 - Divide *" << endl;
cout << "* 5 - Raise X to the power Y *" << endl;
cout << "* 6 - Sine ( x ) *" << endl;
cout << "* 7 - Cosine ( x ) *" << endl;
cout << "* *" << endl;
cout << "* 0 - Quit *" << endl;
cout << "**********************************" << endl;
cout << "Enter a selection, please: " << endl;
Your calculator must:
1. Print the above menu (or one of your own design) and ask the user to select an operation (choices 1 thru 7, or 0). DO NOT change the meaning of input codes (that is: 1 means ADD, 2 means SUBTRACT, and so on…)
2. Based on the user selection, your program must then ask for one or two operands. The operands and the answers will be doubles. userChoice will be an int.
I've made calculators before. You could use the pow() function where 3^2 is pow(3,2), or you could create your own function for powers. I already know what you're going to choose though.
#include <cmath>
cout << "\n Enter Option Number : ";
cin >> userChoice;
resultSin = sin (X*PI/180);
resultCos = cos (X*PI/180);
switch(userChoice)
{
case '1':
cout << "\n Enter Second Number : ";
cin >> Y;
cout << "\n The Result is " << "#### " << (X + Y) << " ####" << endl;
break;
case '2':
cout << "\n Enter Second Number : ";
cin >> Y;
cout << "\n The Result is " << "#### " << (X - Y) << " ####" << endl;
break;
case '3':
cout << "\n Enter Second Number : ";
cin >> Y;
cout << "\n The Result is " << "#### " << (X * Y) << " ####" << endl;
break;
case '4':
cout << "\n Enter Second Number : ";
cin >> Y;
cout << "\n The Result is " << "#### " << (X / Y) << " ####" << endl;
break;
case '5':
cout << "\n Enter Second Number : ";
cin >> Y;
resultPow = pow (X,Y);
cout << "\n Answer = " << "#### " << resultPow << " ####" << endl;
break;
There's actually a thread stickied at the top of this board, it describes how to properly end your program. This should have enough information for you: http://www.cplusplus.com/forum/beginner/1988/