Breaking out of a loop

This is a discussion on Breaking out of a loop within the C Programming forums, part of the General Programming Boards category; I dont know how to break out of two loops when the number input by the user is found. A ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    Breaking out of a loop

    I dont know how to break out of two loops when the number input by the user is found. A user enters a number and it searches the array num[4][8] and when this prints obviously it prints that the number was found or not found 4 times because it only broke the inner loop ONLY when the number has found a match.

    Please help! thanks!
    Code:
    void diff()
    {
     char ans;
     int s;
     printf("\n\nSearch for a number in the array?");
     fflush(stdin);
     scanf("%c", &ans);
     if(ans == 'y' || ans == 'Y')
     {
      printf("\n\nKey in integer to be searched for:");
      fflush(stdin);
      scanf("%d", &s);
      for(suba = 0; suba < 4; suba++)
      {
       for(subb = 0; subb < 8; subb++)
       {
        if(num[suba][subb] == s)
        {
         printf("\n%d is in the array\n", s);
         break;
        }
        else
        {
         printf("\n%d is not in the array\n", s);
         break;
        }
       }
      }
     }
     if(ans == 'N' || ans == 'n')
     {
      eoj();
     }
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    There are two common ways:
    Code:
    /* Set a flag */
    int done = 0;
    
    for ( i = 0; i < 4 && !done; i++ ) {
      for ( j = 0; j < 8; j++ ) {
        if ( something ) {
          done = 1;
          break;
        }
        else {
          /* Keep going */
        }
      }
    }
    Code:
    /* The dreaded goto */
    for ( i = 0; i < 4; i++ ) {
      for ( j = 0; j < 8; j++ ) {
        if ( something )
          goto end;
        else {
          /* Keep going */
        }
      }
    }
    end:
    My best code is written with the delete key.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a third less used way...
    Code:
    for( i = 0; i < foo; i++ )
    {
        for( x = 0; x < bar; x++ )
        {
            for( z = 0; z < foo + bar; z++ )
                if( condition )
                    exit( 0 );
                else
                    dostuff( ";)" );
        }
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    Re

    I think one of these might do it. I will give them a try and let you know how it goes. Thanks you guys! =)

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    Re

    The exit (0); did it, thanks a lot guys!

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    MORE trouble

    I need this to go back to the top and run read() again. I have put them all in one fucntion ebcause its driving me crazy. this assignment never ends. I sure hope this course does! sever e crippling/blinding migraines dont help but I dont question the CAUSE!
    Code:
    void read()
    {
     for(suba = 0; suba < 4; suba++)
     {
      for(subb = 0; subb < 8; subb++)
      {
       x = rand()%51;
       num[suba][subb] = x;
       printf("%4d", num[suba][subb]);
      }
      printf("\n");
     }
     printf("\n\nSearch for a number in the array?");
     fflush(stdin);
     scanf("%c", &ans);
    
     if(ans == 'y' || ans == 'Y')
     {
      printf("\n\nKey in integer to be searched for:");
      fflush(stdin);
      scanf("%d", &s);
      for(suba = 0; suba < 4; suba++)
      {
       for(subb = 0; subb < 8; subb++)
       {
        if(num[suba][subb] == s)
        {
         printf("\n%d is in the array\n\n", s);
         printf("Press enter to continue\n\n");
         fflush(stdin);
         getchar();
         exit(0);
        }
        else
        {
         printf("\n%d is not in the array\n\n", s);
         printf("Press enter to continue\n\n");
         fflush(stdin);
         getchar();
         exit(0);
        }
       }
      }
     }
     if(ans == 'N' || ans == 'n')
     {
      eoj();
     }
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, exit( 0 );, was meant as a joke. It exits the program.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Last edited by Thantos; 04-05-2004 at 01:06 PM.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    18
    I read that. And explained it before. I don't know whether or not I should believe the FAQ. I have no reason to believe its not strapped with more terrorist bombs.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You read it, yet you still use it?

    Bah wth, just noticed it didn't convert the URL. sigh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  2. fgets and breaking out of while loop
    By pollypocket4eva in forum C Programming
    Replies: 25
    Last Post: 01-05-2009, 06:50 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM

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