please help with my calculator! It's not working properly

Can you please help with my calculator? My % isn’t working. I need to get the remainder of 2 integer divisions, but it doesn’t work.

Also, both exponent and square root don’t work properly. I only need the exponent and square root of 1 number but the code asks for 2 number inputs. I don’t know how to format the code so it only asks for 1 number for just these 2 and not for the rest. Please, if anyone can help, I would really appreciate it.

/*
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
cout<<endl;
goto start; // loop back to start
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
cout<<endl;
goto start; // loop back to start
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
cout<<endl;
goto start; // loop back to start
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
cout<<endl;
goto start; // loop back to start
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
cout<<endl;
goto start; // loop back to start
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
cout<<endl;
goto start; // loop back to start
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
cout<<endl;
goto start; // loop back to start
break; // end this section
default:
cout<< "Error in code" <<endl;
break;


}
return 0;
}



Plz change these steps
#include <iostream.h>
#include <math.h>
#include <conio.h>
#include <iomanip.h>

int main ()
{
clrscr();
int f; // here declare int f % give answer in integer

int num1, answer, num2;
char operation;
start:


case '%':
// Don't declare any variable here
f=num1%num2;
cout<<num1<< " the remainder of " <<num2<< " = " <<f<<endl;
cout<<endl;
goto start;
break;
default:
cout<< "Error in code" <<endl;
break;


}
getch();
}
Last edited on
Thank you for the help!
Topic archived. No new replies allowed.