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

This is a discussion on Beginner needs help (calculator for serial/parallel electric circuits) within the C++ Programming forums, part of the General Programming Boards category; I started working on C++ last week and the teacher asked us to write a program that will calculate the ...

  1. #1
    blah!
    Join Date
    Nov 2005
    Posts
    3

    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.
    Last edited by geetard; 11-07-2005 at 07:04 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    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. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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
    Last edited by DougDbug; 11-08-2005 at 12:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21