Getting incorrect output for Parallel Circuit resistance

Hi all,

I'm having trouble with—I believe—getting incorrect output from the second part of the bool statement here. The code's running, and I believe the Series Circuit statment is correct, but not the second part..

Any help would be much appreciated.

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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main() {
    
    char C;
    char S;
    char P;
    double R;
    double R1;
    double R2;
    double R3;
    double I;
    double V=12;
    double Rr;
    
    cout << "Please enter S for Series Circuit or P for Parallel Circuit" << endl;
    cin >> C;
    
    
    if (C == 'S') 	{
        cout << "This program will calculate the total resistance (R) given three resistor values (R1,R2,R3) in ohms" << endl;
        cout << "Please enter the first resistor value (R1) in ohms:" << endl;
        cin >> R1;
        cout << "Please enter the second resistor value (R2) in ohms:" << endl;
        cin >> R2;
        cout << "Please enter the third resistor value (R3) in ohms:" << endl;
        cin >> R3;
        R = R1+R2+R3;
        I = V/R;
        cout << "R = " << R << endl;
        cout << "I = " << I << endl;
    }
    
    else if (C == 'P') 	{
        cout << "This program will calculate the total resistance (R) given three resistor values (R1,R2,R3) in ohms" << endl;
        cout << "Please enter the first resistor value (R1) in ohms:" << endl;
        cin >> R1;
        cout << "Please enter the second resistor value (R2) in ohms:" << endl;
        cin >> R2;
        cout << "Please enter the third resistor value (R3) in ohms:" << endl;
        cin >> R3;
        R = (1/R1) + (1/R2) + (1/R3);
        Rr = (1/R);
        I = V/R;
        cout << "R = " << R << endl;
        cout << "I = " << I << endl;
    }
}
In lines 46 and 47, you're reversed R and Rr.

Parallel resistance formula is: 1/Rtotal = 1/r1 + 1/r2 + 1/r3.
i.e. The result of line 46 is a reciprocal.

46
47
        Rr = (1/R1) + (1/R2) + (1/R3);
        R = (1/Rr);


Last edited on
Lol oh man.

Yeah that fixed it. Thanks so much!
Topic archived. No new replies allowed.