Thread: How to terminate in loop? Help

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    13

    How to terminate in loop? Help

    Code:
      float i, R, I; // counter, resistance, input//
            float V; // voltage//
    
    
            printf("Enter the current: ");
            scanf("%f",&I);
    
    
    
    
    
    
            while ( R != -1) {
    
    
                     
                    printf("Enter the resistance: " );
                    scanf("%f",&R);
                    V= R * I;
                    printf("Voltage is %f\n", V);
            }
    
    
            return 0;
    The question is to write a program that will input resistance R, current I for serval resistors, and will calculate and display the voltage for each resistor. The loop will terminate if your enter -1 as the resistance value. (Note that your program should output nothing when -1 is entered as the resistance value)
    I want it to do nothing when I enter -1 for resistance but for this one it calculates the voltage. So what should i do?
    Last edited by Zijing Ky; 10-15-2012 at 02:51 PM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You could simply put
    Code:
     if(R!=-1){
                  V= R * I;
                  printf("Voltage is %f\n", V);
                  }

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    But that gives me infinite loop of 00000

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    post your new code. You might have put that statement in the wrong place.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    Wait you dont need to use loop for this?

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You tell me... Its your assignment, what are you trying to achieve?

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    OH i editted the starter post.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    When you begin the loop, R has no value (is not initialized) so it's likely that the loop will never execute.

    Also, if the user enters -1 within the loop, the voltage is still calculated and printed before execution returns to the "while()" evaluation, so the rules of your assignment are not satisfied. "(Note that your program should output nothing when -1 is entered as the resistance value)"

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    while( R != -1) 
                               {
                                printf("Enter the resistance: " );
                                scanf("%f",&R);
                               if(R!=-1){
                                          V= R * I;
                                          printf("Voltage is %f\n", V);
                                           }
                             }
    How does this give you an infinite loop?

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    Quote Originally Posted by camel-man View Post
    Code:
    while( R != -1) 
                               {
                                printf("Enter the resistance: " );
                                scanf("%f",&R);
                               if(R!=-1){
                                          V= R * I;
                                          printf("Voltage is %f\n", V);
                                           }
                             }
    How does this give you an infinite loop?
    oh 'cause I didn't add it in, i just replaced it.. Silly me.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    declare and initialize variables
    
    prompt for resistance
    while (resistance is not -1)
        calculate and print voltage
        prompt for resistance

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    Ok it works! Thanks!!

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Take this as a lesson in structuring your logic to neatly accomplish your goals. Figure it out step by step, on paper beforehand if need be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why won't this loop terminate
    By JayCee++ in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2011, 10:00 AM
  2. terminate loop
    By miryellis in forum C Programming
    Replies: 3
    Last Post: 09-07-2004, 07:14 AM
  3. how to terminate for loop
    By Chobo_C++ in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2004, 05:43 PM
  4. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 07:30 AM
  5. How do I terminate a FOR Loop?
    By JYoung in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-14-2001, 03:37 AM