# Thread: Beginner needs help (calculator for serial/parallel electric circuits)

1. ## Beginner needs help (calculator for serial/parallel electric circuits)

I started working on C++ last week and the teacher asked us to write a program that will calculate the total resistance of an electric circuit (both in serial and parallel).

Both serial and parallel work fine, but the thing that bugs me is that I need to block/filter the 0 and negative values so the total resistance in parallel doesn't give me an invalid answer.

I tried the if/else instruction but still doesn't work (I'm sure I did something wrong... gotta read more of my learning book )

I'd appreciate it if you guys can give me a hand here.

Here's my current code (sorry if it looks weird, I translated from french to english):

Code:
```// Excercice #1

#include <iostream>

using std::cout;
using std::cin;

int main()
{
double Rserial,
counterResistors,
Rvalue,
Rparallel,
TRparallel;

Rserial = 0;
counterResistors = 0;
Rparallel = 0;

cout << "Enter the value of the resistor (or enter -1 if done): ";
cin >> Rvalue;

while ( Rvalue != -1 )
{
if ( Rvalue <= 0 )

cout << "\nPlease, enter a valid resistor value! (or enter -1 if done): ";
cin >> Rvalue;
}

else

Rserial = Rserial + Rvalue;
Rparallel = Rparallel + (1 / Rvalue);
TRparallel = 1 / Rparallel;
counterResistors = counterResistors + 1;

cout << "\nEnter the value of the resistor (or enter -1 if done): ";
cin >> Rvalue;
}

cout << "\n*The total resistance in serial is -> R = " << Rserial; cout << " Ohm(s)";
cout << "\n*The total resistance in parallel is -> R = " << TRparallel; cout << " Ohm(s)\n\n";

return 0;
}```

Thanks in advance. PS: In case you forgot your sciences class:

Serial = ValueResistor1 + ValueResistor2 + ValueResistor3 + ... + ValueResistorX
Parallel = 1 / ( 1/VR1 + 1/VR2 + 1/VR3 + ... + 1/VRX) -> Thus why we can't have 0 in this case. 2. You are missing several braces (at the beginning of the if block and around the entire else block).

If that's due to the translation, post the exact untranslated code, we should be able to figure it out. 3. Right - Brackets! You need to put brackets around all the lines that are to be affected by the if-statement. Without brackets, only one line following the if or else is affected.

And, make sure that you have matching-pairs of brackets.

Code:
```while ( Rvalue != -1 )
{
if ( Rvalue <= 0 )
cout << "\nPlease, enter a valid resistor value! (or enter -1 if done): ";  // Depends on if-statement.
cin >> Rvalue;    // This line will execute every time.

else
Rserial = Rserial + Rvalue;  //Depends on else-statement.
Rparallel = Rparallel + (1 / Rvalue);  // Everything else executes every time!
TRparallel = 1 / Rparallel;
counterResistors = counterResistors + 1;

cout << "\nEnter the value of the resistor (or enter -1 if done): ";
cin >> Rvalue;
}```
Code:
```while ( Rvalue != -1 )
{
if ( Rvalue <= 0 )
{    // All of this is affected by the if-statement...
cout << "\nPlease, enter a valid resistor value! (or enter -1 if done): ";
cin >> Rvalue;
}   //End of if-condition.

else
{   // All of this is affected by the else-statement...
Rserial = Rserial + Rvalue;
Rparallel = Rparallel + (1 / Rvalue);
TRparallel = 1 / Rparallel;
counterResistors = counterResistors + 1;

cout << "\nEnter the value of the resistor (or enter -1 if done): ";
cin >> Rvalue;
}  // End of else-condition.

} //End of while-loop``` Popular pages Recent additions 