how do I use static casting in my calculator?

Ok I have a calculator to build for my project. I have it working like a charm. However the requirements have changed to include needing the numbers to be double not int. Setpression(2). That is causing a problem for me as I have to have %. % are requiring me to have the numbers set as int. This is a conflict unless there is a way to get around this issue in C++. Your help is appreciated. This code is what I have so far that works. However I need to adjust it to make numbers double and setpression(2) and include the ability to work with %. I'll also post the requirements.

You will write a program that acts as a simple calculator. The cpp file should be called “calculator_LastNameFirstName.cpp“. The program will read in the numbers (max two) to work on. It will determine which arithmetic operation to use and will display the result. You have to use an if-else statement to pick which arithmetic operation. It will perform the following arithmetic operations:
- Addition, Subtraction, Multiplication, Division, Modulus, Exponents
3. Output from the Calculator
Please input calculation operation eg. (1 + 2): 1 + 2
The answer is:
1 + 2 = 3
Please input calculation operation eg. (1 + 2): 1 - 2
The answer is:
1 - 2 = -1
Please input calculation operation eg. (1 + 2): 1 * 2
The answer is:
1 * 2 = 2




Requirements:
- Code
o Program must use an if-else statement
o Code must be neatly commented and formatted
o Variables names should be descriptive
o Naming convention must be consistent
- Input
o Prompts must clearly let the user know what to do
o Make sure program cannot divide by 0
o Not allow invalid inputs (nulls, random characters, etc) For example, if I type in a letter …
o Must accept whole numbers and decimals (up to two decimal places ~1.25)
- Output
o Each answer must be displayed clearly on a new line
o Must accept whole numbers and decimals (up to two decimal places ~1.25)
o Do not round the result
- General
o Must be user friendly


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

#include <iostream>
#include <iomanip> 
using namespace std;

int main()
{

	std::cout << std::fixed << std::setprecision(2);double choice, value1, value2, result; 

	cout << "My Simple Calculator\n\n";
	
	while(true)
	{
		cout << "1. add\n" << 
		"2. Subtract\n" <<
		"3. Divide\n" <<
		"4. Multiply\n" <<
		"5. Exponents\n" <<
		"6. Modulus\n" <<
		"7. Exit\n\n" <<
		"What would you like to do? ";

		cin >> choice;



			if (choice == 7)
			break;

			cout << "\n Please enter the first value: ";
			cin >> value1;
			cout << "\nPlease enter the second value: ";
			cin >> value2;

			if (choice == 1)
			{
			result = value1 + value2;
			cout << "\nYour answer is: " << value1 << "+" << value2 << "=" <<result<<"\n\n"<<endl;
			}

			else if (choice == 2)
			{
			result = value1 - value2;
			cout << "\nYour answer is: " << value1 << "-" << value2 << "=" <<result<<"\n\n"<<endl;
			}

			else if (choice == 3)
			{
			result = value1 / value2;
			cout << "\nYour answer is: " << value1 << "/" << value2 << "=" <<result<<"\n\n"<<endl;
			}

			else if (choice == 4)
			{
			result = value1 * value2;
			cout << "\nYour answer is: " << value1 << "*" << value2 << "=" <<result<<"\n\n"<<endl;
			}

			else if (choice == 5)
			{
			result = pow(value1, value2);
			cout << "\nYour answer is: " << value1 << "^" << value2 << "=" <<result<<"\n\n"<<endl;
			}

			else if (choice == 6)
			{
				static_cast<int> (result, static_cast<int> (value1, static_cast<int> (value2)
				{
					result = value1 % value2;
				}
			cout << "\nYour answer is: " << value1 << "%" << value2 << "=" <<result<<"\n\n"<<endl;
			}

	}

	cout <<"\n\nThank you for using my simple calculator!"<<endl;

system ("pause");
return 0;
}
Last edited on
make numbers double
just make them double
setpression(2)
Do not forget to set fixed mode: std::cout << std::fixed << std::setprecision(2)
ability to work with %.
http://en.cppreference.com/w/cpp/numeric/math/fmod
Last edited on
value1 and value2 get the red line under them and says

"double value1
error: expression must have integral or unscoped enum type."



what am I doing wrong?

I changed

int choice, value1, value2, result;

to

double choice, value1, value2, result;

then I tried

std::cout << std::fixed << std::setprecision(2)double choice, value1, value2, result;
You are missing semicolon after inserting IO manipulators.
And did you include <iomanip>?
OK I made a few changes. I'm still having problems with using static casting to change the result, num1, and num2 over to int from double.

You can't use double when dealing with Modular. It has to be an int. However, in the requirements everything else deals with doubles.

The professor suggested using static casting to change the doubles to int in the last step of the calculator.

It is not quite right in my code. Still giving me issues. Any idea on what is wrong with it now?

I updated the code on this post.

thanks,
ironsoldier
Last edited on
You can't use double when dealing with Modular. It has to be an int.
fmod to the resque: http://en.cppreference.com/w/cpp/numeric/math/fmod

If you do not want that, you have to cast values at point of their use:
result = static_cast<int>(value1) % static_cast<int>(value2);
Thank you that worked.
Topic archived. No new replies allowed.