How do I show decimals when dividing?

Feb 5, 2017 at 6:48pm
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 Feb 5, 2017 at 6:49pm
Feb 5, 2017 at 6:59pm
One of the numbers you are dividing needs to be a float along with answer.
Feb 5, 2017 at 7:04pm
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;
}
Feb 6, 2017 at 2:46am
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 Feb 6, 2017 at 2:49am
Feb 6, 2017 at 3:57am
Or you could do something like this, which doesn't involve any casts:
answer = 1.0 * a / b;
Feb 6, 2017 at 4:03am
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 Feb 6, 2017 at 4:06am
Feb 6, 2017 at 5:32am
@integralfx

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

Although if a and b were double to start with ....
Feb 6, 2017 at 11:30am
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.