Need help with decimals.

Hi guys, I'm trying to write a program that takes the Voltage, and Current as input, and then generates the Resistance. According to my task both inputs HAS to be integers, the output has to display 3 decimal places, but for some reason the decimals displays ".000" no matter what I do, I want it to display "4.234" instead of "4.000" for example.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int Voltage;
int Current;
double Resistance;

cout << "Enter the Voltage: ";
cin >> Voltage;

cout << "Enter the Current: ";
cin >> Current;

Resistance = (Voltage/Current);

cout << "The resistance is " << fixed << setprecision(3) << Resistance << " ohm" << endl;

return 0;


If the answer should be for example 7.324, it displays the answer as 7.000, how can I fix this?

Thanks for your help.
Last edited on
Voltage and Current should be of type double. Integer division is always an integer, you can't retroactively cast it.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    double Voltage;
    double Current;
    double Resistance;

    cout << "Enter the Voltage: ";
    cin >> Voltage;

    cout << "Enter the Current: ";
    cin >> Current;

    Resistance = (Voltage/Current);

    cout << "The resistance is " << fixed << setprecision(3) << Resistance << " ohm" << endl;

return 0;
}
Thank you, I misunderstood the task slightly, the inputs has to be double aswell, thanks for your help =]
Topic archived. No new replies allowed.