Thread: HOW to back for the beginning if the user press any key except 1 or 2 or 3 ?

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    2

    HOW to back for the beginning if the user press any key except 1 or 2 or 3 ?

    Hello
    In the code bellow the user will enter 1 or 2 or 3 and it will be taken by getch and after that the program will enter to switch ....but i have a problem
    after applying any case of switch i want to make the program back to the beginning if the user press any key ?


    my English is not perfect and i the c language is still new for me so it will be great if you explain in simple way


    Code:
    #include<stdio.h>
     #include<unistd.h>
    #include<stdlib.h>
    #include<math.h>
    #include<conio.h>
     
    int main(void) {
    	 	char L;
    	 
    while (1){
    
    
    		L=getch();
    switch (L) {
    	case '1' :
    	system("cls"); 
        printf("111111111111111")
         break;
    	
    	 case '2' :
    		system("cls");
    	printf("222222222222222" ) ;
    	 ;break;
       case '3' :
    		system("cls");
    	 	printf("33333333");break;
    	  
    	default :
    	sleep(0);
    }
     }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,530
    What do you mean "back to the beginning"?

    The whole program is inside one while(1) loop, which makes it do everything over and over again.

    Additionally, learn to indent code consistently - it's only a few lines long and it's already looking messy.
    Code:
    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<math.h>
    #include<conio.h>
    
    int main(void)
    {
      char L;
    
      while (1) {
        L = getch();
        switch (L) {
        case '1':
          system("cls");
          printf("111111111111111")
              break;
        case '2':
          system("cls");
          printf("222222222222222");
          ;
          break;
        case '3':
          system("cls");
          printf("33333333");
          break;
    
        default:
          sleep(0);
        }
      }
    }
    Line 16 seems to be missing a ;
    Line 21 has an extra ;
    Are you sure this is your real code, and not something you just typed directly into the forum?
    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
    May 2017
    Posts
    2
    Quote Originally Posted by Salem View Post
    What do you mean "back to the beginning"?

    The whole program is inside one while(1) loop, which makes it do everything over and over again.

    Additionally, learn to indent code consistently - it's only a few lines long and it's already looking messy.
    Code:
    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<math.h>
    #include<conio.h>
    
    int main(void)
    {
      char L;
    
      while (1) {
        L = getch();
        switch (L) {
        case '1':
          system("cls");
          printf("111111111111111")
              break;
        case '2':
          system("cls");
          printf("222222222222222");
          ;
          break;
        case '3':
          system("cls");
          printf("33333333");
          break;
    
        default:
          sleep(0);
        }
      }
    }
    Line 16 seems to be missing a ;
    Line 21 has an extra ;
    Are you sure this is your real code, and not something you just typed directly into the forum?
    I found the solution as you said the whole program is inside one while(1) loop, which makes it do everything over and over again so i just make change the default case and it works
    THANKS so much for your interest

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to loop back to the beginning of the function?
    By tivanenk in forum C Programming
    Replies: 4
    Last Post: 10-11-2014, 06:38 PM
  2. Press Enter to go back, press 0 to exit
    By Deanz Tinio in forum C Programming
    Replies: 5
    Last Post: 06-07-2014, 01:01 AM
  3. going back to beginning of file
    By afflictedd2 in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2011, 09:42 AM
  4. moving valuables from classes to functions and back to beginning
    By military genius in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2009, 06:54 PM
  5. Going back to the beginning
    By face_master in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2001, 07:06 PM

Tags for this Thread