trouble creating a calculator which can also calculate %, s, and e.

I’ve created a basic calculator using switch-case programming. There are a few issues with my calculator that I can’t figure out. First, my % (remainder) case isn’t working, and I don’t know what’s wrong with it. Second, I only want to calculate the e (exponent) of one number and also calculate the s (square root) of one number, but it always asked for 2 numbers. When I put (e) num1 and 0 for num2 I get the correct answer. Is that the way I need to do it or is there a better way?


/*
This program is used as a calculator, written in switch-case programming format.
The program can add (+), subtract (-), multiply (*), divide (/), find the remainder
of integer division (%), exponents (e), and square roots (s) of numbers.

The user enters the operator (+,-,*,/,%,e, or s) then enters the first number (num1),
then enters the second number (num2) and the program will calculate the answer (answer) which
will be displayed on the screen. Num2 is not required for exponents or square roots.

After the program computes the result it will loop back to the start point.

num1 - first number to be calculated
num2 - second number to be calculated
answer - the answer to the calculation
+ - addition of two numbers
- - subtraction of two numbers
* - multiplication of two numbers
/ - the division of two numbers
% - the remainders of two integer division
e- the exponent of a number
s- the square root of a number
operation - the operation that's going to be used to calculate the numbers
*/


#include "stdafx.h"
#include <iostream> // needed for cin and cout
#include <cmath> // needed for sqrt and e
#include <iomanip> // needed for formatting

using namespace std;

int main ()
{
double num1, answer, num2; // declaring variables
char operation;

start: // needed for loop. The program will loop back to this point

cout<< "Enter the operation +,-,*, /, e, or s: " <<endl; // telling user to enter the operation
cout<< "Enter the first number: " <<endl; // telling the user to enter the first number for calculations
cout<< "Enter the second number: "<<endl; // telling user to enter the second number for calculations
cout<< endl; // space
cin>> operation >> num1 >> num2; // inputting the operation, first and second numbers
cout<< endl;

switch (operation) // stating switch-case programming

{

case '+': // if user enters + as the operation, this will calculate
answer = num1 + num2; // calculates num1 (plus) num2 = answer
cout<< num1 << " + " << num2 << " = " << answer << endl; // outputting the answer
goto start; // loop back to start
cout<<endl;
break; // end this section

case '-': // if user enters - as the operation, this will calculate
answer = num1 - num2; // calculates num1 (minus) num2 = answer
cout<< num1 << " - " << num2 << " = " << answer << endl; // outputting the answer
goto start; // loop back to start
cout<<endl;
break; // ends this section

case '*': // if user enters * as the operation, this will calculate
answer = num1 * num2; // calculates num1 (multiplied by) num2 = answer
cout<< num1 << " * " << num2 << " = " << answer << endl; // outputting the answer
goto start; // loop back to start
cout<<endl;
break;
case '/': // if user enters / as the operation, this will calculate
answer = num1 / num2; // calculates num1 (divided by) num2 = answer
cout<< num1 << " / " << num2 << " = " << answer << endl; // outputting the answer
goto start; // loop back to start
cout<<endl;
break; // end this section

case 'e': // if user enters e as the operation, this will calculate
answer = exp (num1); // calculates exponent of num1 = answer
cout<< num1 << " exp " << num2 << " = " << answer << endl; // outputting the answer
goto start; // loop back to start
cout<<endl;
break; // end this section
case 's': // if user enters s as the operation, this will calculate
answer = sqrt (num1); // calculates square root of num1 = answer
cout<< num1 << " square root " << num2 << " = " << answer << endl; // outputting the answer
goto start; // loop back to start
cout<<endl;
break;
case '%': // if user enters % as the operation, this will calculate
int num1,num2; // converts num1 and num2 from double to integer
answer = num1 % num2; // calculates the remainder of num1 (divided by) num2 = answer
cout<< num1 << " the remainder of " << num2 << " = " << answer << endl; // outputting the answer
goto start; // loop back to start
cout<<endl;
break; // end this section
default:
cout<< "Error in code" <<endl;
break;
}
return 0;
}

Topic archived. No new replies allowed.