My reverse polish notation calculator calculates the double but not interger?

Hey my program is all set up to calculate but for some reason when i type in the modulus (%)the result is 000 but the rest of the operations work? i could really use some help the program is due tomorrow. I also need to add a bool variable to indicate whether an error has occurred. If the error is true then don’t display the normal output, but instead display an error message. You can also display the error directly from the switch() statement. Thanks!

// Program to use RPN
//
// Author:
//
// Date: 3/4/20
//
#include <iostream>
#include <cmath>
#include <cstdio>


using namespace std;
// precision is done inside printf.
int main()
{
double num1, num2; // 2 variables
char op; // operator
double dResult; // result of doubles
int iResult; // result of intergers

cout << "Enter first number: "; // 1st variable
cin >> num1;

cout << "Enter second number: "; // 2nd variable
cin >> num2;

cout << "Select the operation(+,-,*,%, or 'x' to cancel): ";
cin >> op;

switch (op)
{
case'+':;
dResult = num1 + num2; // Addition of variables
break;

case'-':;
dResult = num1 - num2; // Subtraction of variables
break;

case'*':;
dResult = num1 * num2; // Multiplication of variables
break;

case'/':;
dResult = num1 / num2; // Division of variables
break;

case'%':;
dResult = static_cast<int>(num1) and static_cast<int>(num2); // Modulus of variables
break;

}
if(dResult>1){
printf("The result is: %.2f\n", dResult);
} else{
printf("The result is: %.3d\n", iResult);
}





return 0;
}
Last edited on
Here:
 
dResult = static_cast<int>(num1) and static_cast<int>(num2); // Modulus of variables 

You wrote and but meant to say %.
It's possibly a better choice to call std::fmod instead
dResult = std::fmod(num1, num2)

Don't forget to signal an error if num2 is zero.
ah, a coder after my own heart. When I saw the winx calc mess I wrote one of these too.
I found that a stack for the inputs is very useful, but I am used to a HP11C and sort of expect that functionality.
I also just process what they put in, using that idea, rather than prompt for operators, input like
3
4
5
*
+

= 23

yours isnt tapping the power of RPN because it forces the user to put things into it in a certain order. Its close... just detect if the input is a keyword/operator/etc or a numeric value.
Last edited on
Topic archived. No new replies allowed.