Thread: Help with switch case messing up case variables

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    10

    Help with switch case messing up case variables

    So I have made this and if I switch int instead of char for a
    and make %d my scan as well as use 1 or 2 for case instead of y or n it will make it through the whole program however I have tried to switch my case from 1 and 2 to y and n and it messes up half way. Been on it for a while and maybe I am missing something fresh eyes may help thanks!
    Code:
    #include<stdio.h>
    
    
    int main(void)
    {
     char a;
     int n;
     int y;
     
     
    
    
     
    
    
    
    
    
    
     
    
    
        printf("input answers with y=yes and n=no: Thanks!");
     
        printf("\nAre you between the age of 18 and 26:");
        scanf("%c", &a);
     
     
         switch (a){
         case 'n':
             puts("do not need to register");
             return 0;
         case 'y':
             puts("continue");
             break;
         default:
             printf("Unclear of input");
            break;     
             
          }
         printf("Are you currently on active duty service:");
        scanf("%d", &a);
          
          switch (a){
         case 'y':
             puts("do not need to register");
             return 0;;
         case 'n':
             puts("continue");
             break;
         default:
             printf("Unclear of input");
            }
            
         printf("Are you a midshipmen at service academies or the coast guard:");
         scanf("%d", &a);
          
          switch (a){
         case 'y':
             puts("do not need to register");
             return 0;
         case 'n':
             puts("continue");
             break;
         default:
             printf("Unclear of input");
            
        }
        printf("Are you a student in the Officer Procurment program:");
        scanf("%d", &a);
          
          switch (a){
         case 'y':
             puts("do not need to register");
             return 0;
         case 'n':
             puts("continue");
             break;
         default:
             printf("Unclear of input");
            break;
        }
        printf("Are you a Lawful non-immigrant on current non-immigrant visa:");
     scanf("%d", &a);
          
          switch (a){
         case 'y':
             puts("do not need to register");
             return 0;
         case 'n':
             puts("continue");
             break;
         default:
             printf("Unclear of input");
            break;
        }
        printf("Are you a seasonal agricultural worker (H-2A vis):");
     scanf("%d", &a);
          
          switch (a){
         case 'y':
             puts("do not need to register");
             return 0;
         case 'n':
             puts("continue");
             break;
         default:
             printf("Unclear of input");
            break;
        }
        printf("Are you confined by way of incarceration, hospitilization, and an institution for medical reasons:");
     scanf("%d", &a);
          
          switch (a){
         case 'y':
             puts("do not need to register");
             return 0;
         case 'n':
             puts("continue");
             break;
         default:
             printf("Unclear of input");
            break;
        }
        printf("Are you handicap and continually confined to a hospital or insitution:");
     scanf("%d", &a);
          
          switch (a){
         case 'y':
             puts("do not need to register");
             return 0;
         case 'n':
             puts("continue");
             break;
         default:
             printf("Unclear of input");
            break;
        }
        printf("Are you an individual who are born female and have changed gender to male:");
     scanf("%d", &a);
          
          switch (a){
         case 'y':
             puts("do not need to register");
             return 0;
         case 'n':
             puts("If you have made it through this questionair, you then have to complete the appropiate form and send it in to Selective Services.");
             break;
         default:
             printf("Unclear of input");
            break;
        }
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2018
    Posts
    10
    I saw that in my scanf I left alot of the as %d I have changed that and now it runs 3 of my questions together instead of separate.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,541
    To read the next non-whitespace character using %c, write it as
    Code:
    scanf(" %c",&a);
    The leading space in the format means it will skip over any newlines left over from the end of the previous input.
    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.

  4. #4
    Registered User
    Join Date
    Jul 2018
    Posts
    10
    Quote Originally Posted by Salem View Post
    To read the next non-whitespace character using %c, write it as
    Code:
    scanf(" %c",&a);
    The leading space in the format means it will skip over any newlines left over from the end of the previous input.
    I've been at it for a minute, thanks for the fresh eyes and easy solution!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-09-2016, 03:58 PM
  2. switch case.
    By RyanC in forum C Programming
    Replies: 3
    Last Post: 11-07-2015, 03:22 AM
  3. switch case with variables
    By frs in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2010, 02:07 PM
  4. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  5. switch case
    By Nathan1993 in forum C++ Programming
    Replies: 3
    Last Post: 08-24-2005, 07:16 PM

Tags for this Thread