How do I show decimals when dividing?

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>


using namespace std;

int main()
{
	int a, b, operation;
	
	float answer;
	
	cout << "================== Calculator ==================" << endl << endl;
	
	cout << "Please select your operation: 1-Add, 2-Minus, 3-Multiply, 4-Divide" << endl << endl;

	cin >> operation;
	
	switch(operation)
	
		{
			case (1):
				
				cout << endl;
			
				cout << "++ ADDITION ++" << endl;
				
				cout << endl;
				
				cout << "Enter a number " << endl;
				
				cout << endl;
				
					cin >> a;
				
				cout << a << " + ";
				
					cin >> b;
				
				answer = a + b;
				
				cout << a << " + " << b << " = " << answer;
			break;
			//============================================================
			case (2):
				
				cout << endl;
			
				cout << "-- SUBTRACTION --" << endl;
				
				cout << endl;
				
				cout << "Enter a number " << endl;
				
				cout << endl;
				
					cin >> a;
				
				cout << a << " - ";
				
					cin >> b;
				
				answer = a - b;
				
				cout << a << " - " << b << " = " << answer;
			break;
			//============================================================
			case (3):
				
				cout << endl;
			
				cout << "xx MULTIPLY xx" << endl;
				
				cout << endl;
				
				cout << "Enter a number " << endl;
				
				cout << endl;
				
					cin >> a;
				
				cout << a << " x ";
				
					cin >> b;
				
				answer = a * b;
				
				cout << a << " x " << b << " = " << answer;
			break;
			//============================================================
			case (4):
				
				cout << endl;
			
				cout << "// DIVISION //" << endl;
				
				cout << endl;
				
				cout << "Enter a number " << endl;
				
				cout << endl;
				
					cin >> a;
				
				cout << a << " / ";
				
					cin >> b;
				
				answer = a / b;
				
				cout << a << " / " << b << " = " << answer;
			break;
			
			default:
				cout << " Wrong input.";				
			break;
				
			
		}
	
}


So I made a basic calculator and I'm fine with it except when dividing.

When I divide 3/2 the answer is 1. I want the answer to show decimals, how do I do that?
Last edited on
One of the numbers you are dividing needs to be a float along with answer.
One of the numbers you are dividing needs to be a float

Both numbers can be ints but you need to cast at least one of them to float:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main()
{
    int a , b;

    std::cin >> a;
    std::cin >> b;
    float answer = (float)a/b;
    //float answer = a / (float)b; // alternative
    std::cout << answer;
}
And one should always check for division by zero.

Prefer double rather than float, the latter's precision is easily exceeded. There's no reason why all of the numerical variables cannot be double.

Also, the c++ static_cast<double>(a) is preferred over the C style cast.
Last edited on
Or you could do something like this, which doesn't involve any casts:
answer = 1.0 * a / b;
Nice one, and it works the other way round as well:
double answer = a / (1.0 * b);
edit: interestingly the following also works:
double answer = (1.0 * a) / (1.0 * b);
Last edited on
@integralfx

Good Work!! :+D I hadn't thought of that :+)

Although if a and b were double to start with ....
Although if a and b were double to start with ....
then life would be much simpler but how could we have so much fun then?
Topic archived. No new replies allowed.