Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C programming Do – While loop problems

  1. Oct 28, 2008 #1
    I have to build a program that will find the equivalent resistances in series and parallel of any number of resistances entered at the keyboard. So far i have this:

    I really don't know where i went wrong. The value returned for RTS is the same value as the user entered and the value returned for RTP is the last value for the resistor entered. I reckon it's a problem with the loop but what it is i don't know. Help please?
  2. jcsd
  3. Oct 28, 2008 #2


    User Avatar
    Science Advisor
    Homework Helper

    How many times does the loop execute?
    To test it print the value of x,rts, rtp and loop count inside the loop.

    ps. It's a bad idea to use floating point for loop counters, use integers.
  4. Oct 28, 2008 #3
    It executes as many times as the user entered (The number of resistors they have) because the program compiles and asks for that number of resistances but, for some reason, doesn't seem to add the 'X' value (the resistance entered) correctly. I can't use integers as it will have decimals which could be small and a value of 1 or 0 wouldn't be accurate enough.
  5. Oct 28, 2008 #4


    User Avatar
    Science Advisor
    Homework Helper

    Had to actualy test this - it's a very subtle error.
    The line printf ("blah"),RTS,(1/RTP);

    Is wrong - note the misplaced closing bracket for printf
    what the compiler see is printf("blah blah"); RTS; 1/RTP; As three separate statements.

    Printf is missing the arguements for the %f calls, so it just uses whatever is on the stack next - which happens to be the last values entered in scanf ( because they use the same temporary storage). the extra RTS and 1/RTP statements are just ignored.

    It should be printf ("blah",RTS,(1/RTP));

    If you compiled with warnings on ( eg /Wall in gcc) it shoudl tell you - MS c compiler didn't tell me!

    ps. I mean use integers for counters in the loop, the number of times around the loop MUST be an integer - you can't enter 0.1 resistor values.
  6. Oct 31, 2008 #5
    Thanks for your advice and help, it was great.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: C programming Do – While loop problems
  1. C while loop issue (Replies: 26)

  2. Do while loop (Replies: 6)