Thread: adding a loop

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    5

    adding a loop

    how would I add a loop that would instruct a user to enter a pair of 00
    to end a loop

    Code:
    #include <stdio.h>
    
     int main()
     {
     
     int x; // will define first number 
     int y; // then the second number 
     
     printf( "Please enter first number: " ); // prompt 
     scanf( "%d", &x ); //read first integer 
     
     printf( "Please enter second number: " );
     scanf( "%d", &y ); // read second integer
     // compare the two numbers 
     if ( x > y ) {
     printf( "%d is larger\n", x );
     } 
     if ( x < y ) {
     printf( "%d is larger\n", y );
     } 
     if ( x == y ) {
     printf( "These numbers are equal\n" );
     } 
     return 0; 
     }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you're entering the values in separately, as show in your code, then a simple "do-while" loop would work nicely.

    Code:
    do
    {
        // get first number
        // get second number
        // print any messages
    } while(expression that is false if 'x' and 'y' are both equal to zero);

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    5
    Thanks a lot I'm so new to this
    I try it but it does not work I'M input the two integers but now it locks up
    where in the code would I put this?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Post your updated code and we'll take a look.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    5
    Code:
    #include <stdio.h>
    
     int main()
     {
     
     int x; // will define first number 
     int y; // then the second number 
     
     
     
     printf( "Please enter first number: " ); // prompt 
     scanf( "%d", &x ); //read first integer 
     
     printf( "Please enter second number: " );
     scanf( "%d", &y ); // read second integer
     
     
     // compare the two numbers 
     if ( x > y ) {
     printf( "%d is larger\n", x );
     } 
     if ( x < y ) {
     printf( "%d is larger\n", y );
     } 
     if ( x == y ) {
     printf( "These numbers are equal\n" );
     }
     
     do
    {
        // get first number
        // get second number
        // print any messages
    } while( "expression that is false if 'x' and 'y' are both equal to zero: ");
     
     return 0; 
     }

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I did not hand you the actual working code to copy + paste into your source - that was a template for you to fill in based on what you wanted.

    1. Where do you want the loop to start from? (In other words, which is the first instruction you want to execute each time the loop iterates)
    2. Where do you want the loop to end? (In other words, which is the last instruction you want executed before looping back to the top of the loop)

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    5
    Like I said I'm new to this thanks for your input
    I will fig out hopefully
    Thanks again

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I wasn't asking for you to put that in code, but instead to just answer in words.

    Why don't you check out the link I posted above, start a fresh project, and tinker around with a "do-while" loop until you get the hang of it. Then incorporate it into this program. Someone will likely be here to help you if you get stuck along the way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding values to array in a for loop
    By bman900 in forum C Programming
    Replies: 1
    Last Post: 11-06-2010, 06:58 AM
  2. Lost in a loop, sum not adding up correctly... Please help.
    By matthayzon89 in forum C Programming
    Replies: 1
    Last Post: 09-26-2010, 03:48 PM
  3. Adding an array using a For loop
    By C++Noob316 in forum C++ Programming
    Replies: 11
    Last Post: 03-12-2009, 01:13 PM
  4. Adding output of a FOR loop
    By cjohnson412 in forum C++ Programming
    Replies: 9
    Last Post: 06-22-2008, 07:41 PM
  5. help with storing the last value of a loop and adding it?
    By colmustang in forum C Programming
    Replies: 4
    Last Post: 03-08-2008, 09:31 AM