Thread: Question related to returning to earlier parts of the code.

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    Question related to returning to earlier parts of the code.

    I was wondering if it was possible to return to a previous part of the code. I will use a small example. I apologize if this is a n00by question.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
         int selection;
         char color[10];
         int bunk;
    
         printf("Select what you would like to do\n\n");
         printf("\n\n1.) Tell me about colors");
         printf("\n2.) The second option");
         printf("\n\nEnter command: ");
         scanf("%d", &selection);
    
         switch (selection) {
             case 1:
                  printf("\n\nwhich color would you like to know about?\n\n");
                  scanf("%s", color);
                  break;
             case 2:
                  printf("\n\noh hello");
                  break;
                  }
    
        if( strcmp( color, "green" ) == 0 ) {
            printf("\n\nGreen is made up of both blue and yellow");
    }              
                    
        scanf("%d", bunk);
               
        getchar();
    }
    How would I get the program to return to the switch case (the selection with the int input)?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
       do {
         printf("\n2.) The second option");
         printf("\n\nEnter command, or zero to quit: ");
         scanf("%d", &selection);
    
         switch (selection) {
             case 1:
                  printf("\n\nwhich color would you like to know about?\n\n");
                  scanf("%s", color);
                  break;
             case 2:
                  printf("\n\noh hello");
                  break;
                  }
    
        if( strcmp( color, "green" ) == 0 ) {
            printf("\n\nGreen is made up of both blue and yellow");
    
      }while(selection != 0);
    }              
                    
        scanf("%d", bunk);
               
        getchar();
    }
    Something like that. Just using a loop.

    And welcome to the forum - thanks for using code tags, also.
    Last edited by Adak; 03-13-2010 at 10:08 PM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    Thank you very much. And no problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A rather broad code design question
    By Walker in forum C++ Programming
    Replies: 6
    Last Post: 01-27-2010, 07:09 PM
  2. Linked List Tutorial sample code question
    By Kalleos in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 12:20 PM
  3. char* returning question
    By Devil Panther in forum C Programming
    Replies: 19
    Last Post: 08-29-2005, 12:50 PM
  4. Completely seperating parts of code
    By Boksha in forum C++ Programming
    Replies: 3
    Last Post: 08-14-2005, 11:36 AM
  5. Newbie Source Code question. Hlp Plz?
    By BOOGIEMAN in forum Game Programming
    Replies: 4
    Last Post: 12-14-2001, 04:30 AM

Tags for this Thread