Thread: my switch stamement is not working?

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    1

    my switch stamement is not working?

    okay so like case 2 and 3 is not working, also the program is still not finish but why isn't case 2 and 3 not functioning well on the switch part? case 1 works just fine :/ please help!!!
    Code:
    #include<stdio.h>
    main ()
    {
         //passcode part (passcode is 1234)
         int passcode, choice, choice1;
         char letter;
         
         system("color 49");
         
         printf("\n\n\n\n\n\t\t      The passcode is a 4 - digit number.");
         printf("\n\n\n\n\n\n\t\t\tEnter the PASSCODE >>> ");
         scanf("%d", &passcode);
         while(passcode != 1234)
         {
                 system("cls");    
                 printf("\n\n\n\n\n\t\tYou entered the wrong passcode! Please try again.");
                 printf("\n\n\n\n\n\n\t\t\tEnter the PASSCODE >>> ");
                 scanf("%d", &passcode);
         }
         system("cls");   
         printf("\n\n\n\n\n\n\t      Congratulations, you have entered the right passcode.");
         printf("  \n\n\n\n\n\tDo you still want to continue? Type [1] for yes and [2] for no >>> ");
         scanf("%d", &choice);
         if(choice == 1)
         {
                   system("cls");
                   //da menu lol
                   printf("\n----------------------------------------------------------------------------");
                   printf("----------------------------------------------------------------------------");
                   printf("----------------------------------------------------------------------------------------");
                   
                   printf("\t\t    ****************************************");
                   
                   printf("\n\n\t\t      HEALTY FITNESS PROGRAM w/ MATH v.12");
                   printf("\n\n\t\t\t1. Information");
                   printf("\n\n\t\t\t2. Statistics");
                   printf("\n\n\t\t\t3. About the Creators");
                   printf("\n\n\t\t\t4. Fun, Special and Interesting!");
                   printf("\n\n\t\t\t5. Exit");
                   
                   
                   printf("\n\n----------------------------------------------------------------------------");
                   printf("----------------------------------------------------------------------------");
                   printf("----------------------------------------------------------------------------------------");
                   
                   printf("\n\t\t\t   Select a number >>> ");
                   scanf("%d", &choice1);
                   switch(choice1)
                   {
                                   case 1:
                                         system("cls");
                                         printf("\n----------------------------------------------------------------------------");
                                         printf("----------------------------------------------------------------------------");
                                         printf("----------------------------------------------------------------------------------------");
                                         
                                         printf("\n\t-+INFORMATION ABOUT THE PROGRAM");
                                         printf("\n\nThis program is simply an array of statistical tools that calculates the set of values which is the health data of a student.");
                                         printf("\n\n\t-+STATISTICAL TOOLS");
                                         printf("\n\nThis program offers solving the mean, median, mode, range, standard deviation and percentiles of a health data of a student.");
                                         printf("\n\n\t-+STATISTICS");
                                         printf("\n\nThis is where all the calculations take place, the main purpose of the program.");
                                         printf("\n\n\t-+ABOUT THE CREATORS");
                                         printf("\n\nThis is where all the details on the creators of the program are placed, check \nit out!");
                                         printf("\n\n\t-+FUN, SPECIAL AND INTERESTING!");
                                         printf("\n\nThis is an extra part of the program, can burn your boredom to ashes thats why \nit has an exclamation mark on it.");
                                         
                                         printf("\n\n----------------------------------------------------------------------------");
                                         printf("----------------------------------------------------------------------------");
                                         printf("----------------------------------------------------------------------------------------");
                                         
                                         printf("\n\t\t Wanna go back? Type [Y] for yes or [N] for no. >>> ");
                                         scanf("%s", &letter); 
                                           if(letter == 'y' || letter == 'Y') 
                                            main (); 
                                         else 
                                         system("cls");  
                                         printf("\n\n\n\n\n\n\t     The program will now terminate... Thank you for using!\n\n\n\n\t\t\t  Press any key now to close!");
                                         break;
                                   case 2:
                                         printf("\n----------------------------------------------------------------------------");
                                         printf("----------------------------------------------------------------------------");
                                         printf("----------------------------------------------------------------------------------------");
                                         printf("\n\t-+INFORMATION ABOUT THE PROGRAM");
                                         printf("\n\nThis program is simply an array of statistical tools that calculates the set of values which is the health data of a student.");
                                         break;
                                   case 3:
                                         printf("\n----------------------------------------------------------------------------");
                                         printf("----------------------------------------------------------------------------");
                                         printf("----------------------------------------------------------------------------------------");
                                         printf("\n\nThis program is simply an array of statistical tools that calculates the set of values which is the health data of a student.");
                                         break;
                   }                                                                                                            
         }
         else
         {
                   system("cls");
                   printf("\n\n\n\n\n\n\t     The program will now terminate... Thank you for using!\n\n\n\n\t\t\t  Press any key now to close!");
                   getch();
         }
         
         
         
         
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,867
    Please try to never use the phrase "not working"!
    What is it doing?
    What do you think it should be doing, instead?
    What input are you giving it?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3,867
    Never call the function main inside a C program!!!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,784
    Speaking of C program, is this supposed to be C or C++? You posted in the C++ programming forum, but your program is composed entirely of constructs from C.

    That said, in C, it is legal to call the main function from within the program, though it is generally still discouraged.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    That said, in C, it is legal to call the main function from within the program, though it is generally still discouraged.
    In this case, depending on what the OP means by "not working", it is probably the cause of the problem. In effect, calling main() is being used as a glorified goto or to loop, except that it will re-execute code at the start of main() which the OP doesn't want executed again.

    Compiling as C++ would have been preferable, as the compiler would have rejected the code. Which, frankly, is a better outcome of this code than running it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char letter;
    ...
    printf("\n\t\t Wanna go back? Type [Y] for yes or [N] for no. >>> ");
    scanf("%s", &letter);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch loop not working
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 10-29-2008, 12:54 PM
  2. case switch not working
    By AmbliKai in forum C Programming
    Replies: 2
    Last Post: 10-09-2008, 06:42 AM
  3. Switch structure not working
    By him61 in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2007, 05:35 PM
  4. why isnt my switch working
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-21-2002, 12:08 PM
  5. switch not working
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2002, 07:25 AM