Thread: Break the Loop.

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    Karachi, Pakistan
    Posts
    19

    Break the Loop.

    Hello everyone...!

    This programme is a very simple one which prints a list of options from which the user is asked to select one. After the user inputs his selection, the programme prints it and again shows the list to select from. It should keep on going until the user types "quit". But actually it continues the loop whether you type quit or not and does'nt come to end until you Ctrl/Break it yourself. Please go through the source code below and mark the mistake for me.

    Code:
    /*Ex 8.3*/
    /*prints the selected option from a menu*/
    
    #include<string.h>
    #include<stdio.h>
    #define MAX 7
    #define LEN 15
    
    int main(void)
    {
     char menu[MAX][LEN]={ "RL series",             /*Array initialization*/
    		       "RC series",
    		       "LC series",
    		       "RL parallel",
    		       "RC parallel",
    		       "LC parallel",
    		       "quit" };
     char optn[BUFSIZ],out[BUFSIZ],i;               /*declares input and output arrays*/
     system("cls");
     do
     {
      for (i=0;i<MAX;i++)                          /*prints the menu*/
    	printf("%s\n", menu[i]);
    
      printf("Enter option >>");
      gets(optn);                                  /*gets the option selected*/
      for (i=0;i<MAX;i++)                          /*go throughs the menu array*/
      {
       if( strcmp(&menu[i][0],optn) == 0 )                  // if input matches with the
           {                                                // menu option...
    	sprintf(out,">>>%s option selected",&menu[i]);  // puts the output into out[]
    	puts(out);               /*prints the output*/
           }                                       
      }
     }
     while( strcmp(&menu[6][0],out) != 0);         /*exits loop if the output is "quit"*/
     system("pause");
     return(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > sprintf(out,">>>%s option selected",&menu[i]); // puts the output into out[]
    > while( strcmp(&menu[6][0],out) != 0); /*exits loop if the output is "quit"*/
    Maybe compare with the input, not the output?

    Also, hasn't anyone told you about gets() yet?

    Also, &menu[6][0] is more simply written as menu[6]
    Arrays have a little wiggle room in the syntax you can use, which you simply can't use when you start using pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    Karachi, Pakistan
    Posts
    19
    > sprintf(out,">>>%s option selected",&menu[i]); // puts the output into out[]
    > while( strcmp(&menu[6][0],out) != 0); /*exits loop if the output is "quit"*/
    Maybe compare with the input, not the output?
    Salem that worked...really!!
    Are you a witch-doctor!!??
    ...your name suggests it though, and your problem-solving skills too.

    Well, jokes aside. So tell me what has anyone not told me about gets()??

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Lord Slytherin View Post
    Salem that worked...really!!
    Are you a witch-doctor!!??
    ...your name suggests it though, and your problem-solving skills too.

    Well, jokes aside. So tell me what has anyone not told me about gets()??
    You should read: Why gets() is bad-FAQ
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to break out of loop
    By Ducky in forum C++ Programming
    Replies: 31
    Last Post: 09-07-2010, 11:22 PM
  2. how could I break this loop?
    By Trafalgar Law in forum C Programming
    Replies: 4
    Last Post: 09-27-2008, 05:11 AM
  3. Can you break; for for loop?
    By DavidDobson in forum C Programming
    Replies: 4
    Last Post: 09-24-2008, 12:59 PM
  4. Loop will not break.
    By silhoutte75 in forum C Programming
    Replies: 1
    Last Post: 03-20-2008, 06:51 PM
  5. Need help with break in do while loop
    By JoelearningC in forum C Programming
    Replies: 3
    Last Post: 03-19-2008, 11:12 PM