DC Circuits Program

My group and I are writing a program that can solve Resistance total, Voltage total and Current total in a DC series, parallel, and series-parallel circuit. We have the whole program written already but we cannot get the parallel part of the program to work. We have the program setup to have up to 5 resistors for series and 5 for parallel, the series part works perfectly and give you the correct total resistance. When you enter the resistance values for the parallel part (lines 73 to 82)of the circuit it gives us the incorrect total resistance if you enter values for less than 5 resistors. Example if the circuit only has 3 resistors in parallel you enter the 3 values and the other 2 resistors you have to enter 0’s for the values and it will give you the incorrect resistance total. If you enter 5 resistor values it gives us the correct number. We think the reason we are getting the incorrect value is because you have to enter a 0’s.

We need help in figuring what we can do to get the parallel part to work. We tried rewriting the program to first ask the user how many resistors are in parallel to avoid entering 0’s but we could not get it to work probably because we are all beginners in programming. Any assistance is 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
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
 #include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
void resistance();
void current();
void voltage();
void printMenu(void);
int main()

{
char selection;
do

{
printMenu();
cin>>selection;

switch(selection)

{
case 'r':
case 'R':
resistance();
break;

case 'c':
case 'C':
current();
 break;

 case 'v':
case 'V':
voltage();
 break;

 case 'q':
case 'Q':
cout<<"Thank you & goodbye."<<endl;

return 0;
}}
 while(selection != 'r' ||selection != 'R' ||selection != 'c' ||selection != 'C'


||selection != 'v' ||selection != 'V' ||selection != 'q' ||selection != 'Q');

 cout<<"Invalid selection."<<endl;
return main();
}

void printMenu()
{

	cout<<"[R]esistance [C]urrent [V]oltage [Q]uit"<<endl;
}

void resistance()
{

	const int maxResRS = 5, maxResRP = 5;
float Res[maxResRS], res[maxResRP];
float resSTot = 0, resPTot = 0, resTot = 0;
cout<<"Enter the value of up to 5 resistors in series:"<<endl;
for(int i = 0; i < maxResRS; i++)

{

cin>>Res[i];
resSTot += Res[i];
}

cout<<"Enter the value of up to 5 resistors in parallel:"<<endl;
for(int j = 0;j < maxResRP; j++)
{

cin>>res[j];
resPTot += pow(res[j],-1);
resTot = resSTot+pow(resPTot,-1);
}

cout<<"\n The total resistance ="<<resTot<<endl;

}
void current()

{

int resTot, Volt_T;
cout<<"Please enter the total resistance of the circuit:"<<endl;
cin>>resTot;
cout<<"Please enter the total voltage:"<<endl;
cin>>Volt_T;
cout<<"The total current ="<<Volt_T/resTot<<endl;

}

void voltage()
int resTot, Curr_T;
cout<<"Please enter the total resistance of the circuit:"<<endl;
cin>>resTot;
cout<<"Please enter the total voltage:"<<endl;
cin>>Curr_T;
cout<<"The total current ="<<Curr_T/resTot<<endl;

}


I think your problem is in your math. Does 0^-1 = 0?
Yes, we figured that was the problem but we tried different ways to get it to work yet nothing. we are out of ideas.
Unless you need to remember the individual values, you don't really need to use an array in the first place which is forcing you to use 0 if < 5 values are entered. You could still test if res == 0 then add 0 to resPTot, else add res^-1 to resPTot though. If you don't need to save the values, you could just keep adding them to the total until the user said they were done or, if you do, you could use a vector.
Topic archived. No new replies allowed.