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