Like Tree1Likes
  • 1 Post By nonoob

Switch problem

This is a discussion on Switch problem within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> int main() { char username[16]; char pass[16]; char conf_pass[16]; short int condition = 0; short ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    39

    Angry Switch problem

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       char username[16];
       char pass[16];
       char conf_pass[16];
       short int condition = 0;
       short int i = 0;
       short int j = 0;
       int opt_no = 0;
       
       printf("\n**************ADMISSION*PROGRAM*FOR****************");
       printf("\n********************STUDENTS***********************");
       
       printf("\n\n");
       
       printf("\n1.LOG IN");
       printf("\n2.CREATE ACCOUNT");
       
       printf("\nEnter Option No.:\t");
       scanf("%d", &opt_no);
       
       switch(opt_no)
       {
       
          case '1':
                break;
                
          case '2':      
       
                printf("\nEnter Desired Adiminisrator User Name:\t");
                scanf("%s", username);
       
      
                printf("(Enter Desired Password Not Less Than 8 Characters");
                printf("\nAnd Not More Than 16 Characters.)");
       
                while(condition == 0) 
                {
          
                   printf("\n\nPassword:\t\t");
                   scanf("%s", pass);
                   j = strlen(pass);
                   if(j < 8)
                   {
                      printf("\nError.Password is less than 8 characters.");
                   }
                   if(j > 16)
                   {
                      printf("\nError.Password is more than 16 characters.");
                   }
                   if(j >= 8 && j <= 16)
                   {
                      printf("Confirm Password:\t");
                      scanf("%s", conf_pass);
                      i = strcmp(pass, conf_pass);
             
                      if(i != 0)
                      {
                         printf("Error! Passwords do not match");
                      }
                      if(i == 0)
                      {
                         condition = 1;
                      }
                   }
          
                }   
       }
       return 0;
    }
    switch not working.
    PS: Code not finished, need a little help NOT HOMEWORK.
    PPS: New to programming.
    Last edited by GolDRoger; 02-09-2012 at 06:20 AM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > switch not working
    Be more specific?

    > NOT HOMEWORK
    Doesn't matter, I'd still help you if it was, because you made an effort to solve it yourself.

    As for your code, split it into separate functions, and not use a switch statement. I would completely scrap the magic numbers as well:
    Code:
    #define OPT_LOGIN          '1'
    #define OPT_REGISTER     '2'
    
    /* ..... */
    
    printf("\n%c. LOG IN", OPT_LOGIN);
    printf("\n%c. CREATE ACCOUNT", OPT_REGISTER);
    
    /* ..... */
    
    char c = getchar();
    
    if (c == OPT_LOGIN) {
        /* do stuff */
    } else if (c == OPT_REGISTER) {
        /* do more stuff */
    } else {
        /* invalid option */
    }
    Getchar(), while much better than scanf for single character input, does screw up the input buffer, so make sure you deal with the newline character appropriately.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    Code:
     scanf("%d", &opt_no);
    Instead of scanf use getchar.

    Code:
     opt_no=getchar();
    And switch statement will work.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Or leave input as it is, change cases to integers ...
    case 1:
    ...
    case 2:
    ...
    Stevan likes this.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    You could also change opt_no to char, cause '1' is type char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch Problem
    By ahhak1989 in forum C Programming
    Replies: 35
    Last Post: 04-25-2009, 08:58 AM
  2. switch problem
    By archriku in forum C Programming
    Replies: 21
    Last Post: 03-28-2009, 08:31 AM
  3. switch problem
    By joshhud in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 11:35 AM
  4. problem on switch
    By toxicherry in forum C Programming
    Replies: 11
    Last Post: 12-31-2007, 04:17 AM
  5. Switch Problem
    By Tynnhammar in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2004, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21