Building a calculator

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.
You are probably expected to use a switch statement ie.
1
2
3
4
5
6
cin >> userChoice
 switch(userChoice){
   case '1':
       //addition
....
}
This video shows how to make a basic calculator.

http://youtu.be/yjucJUsHSqg
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>

Use this for sine and cosine. It shouldn't take you long now.
http://www.cplusplus.com/reference/clibrary/cmath/sin/
ok so this is what i have so far:

#include <iostream>
#include <math.h>


using namespace std;

#define PI 3.14159265

int main()
{


double X, Y;
double resultPow, resultSin, resultCos;
char userChoice;

cout << "**********************************" << endl;
cout << "* 1 - Add *" << endl;
cout << "* 2 - Subtract *" << endl;
cout << "* 3 - Multiply *" << 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 << "\n Enter First Number : ";
cin >> X;

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;

case '6':
cout << "\n Answer = " << "#### " << resultSin << " ####" << endl;
break;

case '7':
cout << "\n Answer = " << "#### " << resultCos << " ####" << endl;
break;

case '0':
cout<< " Are you sure you want to quit?" <<
cin.get();
}
return 0;
}




However when I choose the "are you sure you want to quit" option, it ends the program no matter what i press. what am i doing wrong?
everything else in the program works fine btw
Switch case is not a loop. It runs once and it's done. You will want to put it in a while loop, if you want to execute more than once.

cin.get(); needs a variable or it's just empty input. The input goes nowhere.

You need something to tell the user what to enter if they want to stay or quit, and then check that to see if they want to stay or not.
would you know how to do that? i tried looking for a way to do it but i cant find it
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/
char userChoice = 'n';
while(tolower(userChoice) != 'y')
{
output and everything else
cout << "Do you really want to quit? (y/n): ";
cin >> userChoice;
}
}

This will loop until the user wants to quit and says yes.
im getting 2 errors doing that grex

lab31.cpp:16: warning: implicit declaration of function `int tolower(...)'
lab31.cpp:89: no match for `ostream & >> char &'
#include <ctype.h>

int tolower... you don't use int
Last edited on
if userChoice is a char, then it shouldn't be a problem. If you changed it to an int, then you need to make another char to replace user choice with.
Topic archived. No new replies allowed.