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.