Thread: looping quest..

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    15

    looping quest..

    i have a a switch statement with about 10 cases. What i want the program to do is pretty much run the entire program again. I tried not putting in the break at the end of each case, but the program ends up doing all the cases. n

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    do u mean soemthing like this

    Sampe code
    Code:
    #include<stdio.h>
    
    int main()
    {
     	int ch;
     	
     	printf("1. Add values\n");
     	printf("2. Sub values\n");
    	printf("3. Mul Values\n");
    	printf("4. Exit\n");
    	
    	printf("\nEnter your choice\n");
    	scanf("%d",&ch);
    	
    	while(ch!=4)
    	{
    	 		switch(ch)
    			 {
    				  case 1:
    				   	 /*do something here*/
    			 break;
    			      case 2:
    					/* do something here*/
    			 break;
    				case 3:
    				                /* do soemthing here */
    			 break;
    			 }
    				 printf("1. Add values\n");
     				 printf("2. Sub values\n");
    			 	 printf("3. Mul Values\n");
    				 printf("4. Exit\n");
    	
    				 printf("\nEnter your choice\n");
    				 scanf("%d",&ch);
        }
        
        getchar();
        return 0;
    }
    ssharish2005
    Last edited by ssharish2005; 11-20-2005 at 10:30 PM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Nice indentation, ssharish2005! And did you ever consider using a do-while loop or something to eliminate the duplicate code?

    I would re-write the above code to look like this:
    Code:
    #include <stdio.h>
    
    int main(void) {
        int ch, c
    
        while(ch != 4) {
            printf("1. Add values\n");
            printf("2. Sub values\n");
            printf("3. Mul Values\n");
            printf("4. Exit\n");
    	
            printf("\nEnter your choice\n");
            scanf("%d",&ch);
    
            while((c = getchar()) != '\n' && c != EOF);
    
            switch(ch) {
            case 1:
                /*do something here*/
                break;
    
            case 2:
                /* do something here*/
                break;
    
            case 3:
                /* do something here */
                break;
    
            default: break;
            }
        }
    
        getchar();
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    int main(void) {
        int ch, c
    
        while(ch != 4)
    ch contains the garbage value.It may contain 4.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'd use dwks' solution but with a do-while loop instead to eliminate the chance that ch starts off with the value 4:
    Code:
    do
    {
    // blah blah blah
    } while(ch != 4);
    If you understand what you're doing, you're not learning anything.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oops, yeah, I forgot a semicolon and stuff, the line was supposed to be like this:
    Code:
    int ch = 0, c;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  2. MoveToEx() & LineTo() functions
    By csonx_p in forum Windows Programming
    Replies: 17
    Last Post: 04-11-2008, 12:53 AM
  3. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  4. Quest for the Quests
    By sean in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-16-2002, 12:18 AM
  5. Space, Police, and King's quest.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 07-02-2002, 12:33 PM