Thread: Unreachable code?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    103

    Unreachable code?

    Hii!
    My compiler says:
    Unreachable code at beginning of switch statement.
    Does that mean that only the first case is not reached??
    Because I've deleted all statements within the first case except for the break, and yet the problem still occurs.
    Any idea?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So you have code between where it says switch() and the first case label?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In other words:
    Code:
    switch (foo) {
        printf("This line will never happen.\n");
        case 1:
             //etcetera
    }

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    No.the first case is in its appropriate place.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You could always post the code, so we can stop guessing and get to the point of actually telling you what's wrong.
    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.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Leojeen View Post
    No.the first case is in its appropriate place.
    Then you'll have to show us.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok
    Here is the code.
    Code:
    switch(choice1){
                                         case 1:
                                                printf("\nHow many accounts you want to add?");
                                                scanf("%d",&choice2);
                                                account_number += choice2;
                                                account temp[account_number];
                                                for (int i = 0 ; i < choice2 ; i++)
                                                    temp [i] = add_account();
                                                for (int j = choice2 ; j < account_number ; j++)
                                                    fread(&temp[j],sizeof(account),1,ptr);
                                                sort(temp,account_number);
                                                for(int k = 0 ; k < account_number ; k++)
                                                        fwrite(&temp[k],sizeof(account),1,ptr);
                                                break;
                                         case 2:
                                                if(!account_number){
                                                                    printf("Cannot delete account.Your database is empty\n.");
                                                                    exit(1);
                                                                    }
                                                account temp2[account_number];
                                                do{
                                                     printf("\nType the account's corresponding user name.\n");
                                                     account temp;
                                                     fgets(temp.user_name,MAX1,stdin);
                                                     if(account_number){
                                                                       delete_account(&temp,account_number);
                                                                       account_number--;
                                                                       }
                                                     else{
                                                         printf("Cannot delete. Database already empty.\n");
                                                         exit(1);
                                                         }
                                                     printf("Again?(1 to remove more, another key to exit):");
                                                     scanf("%d",&choice3);
                                                     }while(choice3 == 1);
                                                for (int j = 0 ; j < account_number ; j++)
                                                         fread(&temp2[j],sizeof(account),1,ptr);
                                                sort(temp2 , account_number);
                                                for(int k = 0 ; k < account_number ; k++)
                                                        fwrite(&temp2[k],sizeof(account),1,ptr);
                                                break;
                                         case 3:
                                                do{
                                                   if(!account_number){
                                                                       printf("Database is empty.\n");
                                                                       exit(1);
                                                                       }
                                                   printf("\nType the account's corresponding user name.\n");
                                                   account temp;
                                                   fgets(temp.user_name,MAX1,stdin);
                                                   change_password(&temp);
                                                   printf("Password changed.\n");
                                                   printf("again?(1 to change more passwords, any other key to exits)",&choice4);
                                                   }while(choice4 == 1);
                                         case 4 :
                                                do{
                                                   account temp;
                                                   printf("Enter the name of the account.\n");
                                                   fgets(temp.user_name,MAX2,stdin);
                                                   display(&temp,ptr);
                                                   printf("More details?(1 to confirm, else to exit):");
                                                   scanf("%d",&choice5);
                                                   }while(choice5 == 1);
                                         case 5:
                                                printf("You existed the program.\n");
                                                fclose(ptr);
                                                return 0; 
                                         default:
                                                 printf("Invalid Input.\n");
                                         }

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps the switch itself is not reached?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > for (int i = 0 ; i < choice2 ; i++)
    Given that this isn't valid C code (it's valid C99 code), the compiler probably turned it into this
    Code:
    switch ( choice1 ) {
        int i;
        case 1:
    }
    and then promptly complained about it.

    Try something like this instead, to stop the propagation of the declaration.
    Code:
    switch ( choice1 ) {
        case 1: {
          // all your case 1 code here.
        } break;
    }
    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.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    But, the integer i has nothing to do with the case variable. it exits only when I'm looping, and that is I need it for.
    and to make clearer, I removed all the code in between the case label (the first one) and the break statement..and the problem is still there.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Guys, I need little help.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Leojeen View Post
    Guys, I need little help.
    We can't give you any help because the code you posted doesn't have any errors. You can make sure that the code you posted is the code you're compiling, and that you've got the line number right for the error and you're on the right switch. What IDE & compiler are you using, and what actual error message are you getting?

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok.
    I'm using Dev - C++ 4.9.9.2
    and the problem corresponds to the switch line.

    The errors are:

    In function `int main()':

    [Warning] unreachable code at beginning of switch statement

    Hope that helps

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So I copied that code that you posted in Dev-C++, found the declaration of your struct in another thread, commented out all the function calls that I didn't have, and I got the following:
    temp.c:63: warning: too many arguments for format
    (that's the line with a printf that ends in &choice4). Nothing about unreachable code (I made sure to add -Wunreachable-code to be sure).

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok, that may appear long, but have a look at this.
    (the problem is still there).
    I know there is a function that I haven't implemented yet. (delete).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <cctype>
    #define MAX2 20
    #define MAX1 5
    typedef struct{
                   char user_name[MAX2],password[MAX2];
                   int privilige;
            }account;
    void display(account *,FILE *);
    account add_account();
    void delete_account(account *, int);
    void change_password(account *);
    void sort (account [] , int);
    int main(){
               int account_number = 0,choice1,choice2,choice3,choice4,choice5;
               FILE *ptr;
               if((ptr = fopen("accounts.bin","r+")) || (ptr = fopen("accounts.bin","w"))){
                      do{
                         printf("Ready to manipulate your data.\n");
                         printf("\t\tTo add accounts, press 1.\n");
                         printf("\t\tTo delete an account, press 2.\n");
                         printf("\t\tTo change a password, press 3.\n");
                         printf("\t\tTo display details of an account, press 4.\n");
                         printf("\t\tTo exit, press 5.\n");
                         printf("\t\t\tYour choice?");
                         scanf("%d",&choice1);
                         switch(choice1){
                                         case 1:
                                                printf("\nHow many accounts you want to add?");
                                                scanf("%d",&choice2);
                                                account_number += choice2;
                                                account temp[account_number];
                                                for (int i = 0 ; i < choice2 ; i++)
                                                    temp [i] = add_account();
                                                for (int j = choice2 ; j < account_number ; j++)
                                                    fread(&temp[j],sizeof(account),1,ptr);
                                                sort(temp,account_number);
                                                for(int k = 0 ; k < account_number ; k++)
                                                        fwrite(&temp[k],sizeof(account),1,ptr);
                                                break;
                                         case 2:
                                                if(!account_number){
                                                                    printf("Cannot delete account.Your database is empty\n.");
                                                                    exit(1);
                                                                    }
                                                account temp2[account_number];
                                                do{
                                                     printf("\nType the account's corresponding user name.\n");
                                                     account temp;
                                                     fgets(temp.user_name,MAX1,stdin);
                                                     if(account_number){
                                                                       delete_account(&temp,account_number);
                                                                       account_number--;
                                                                       }
                                                     else{
                                                         printf("Cannot delete. Database already empty.\n");
                                                         exit(1);
                                                         }
                                                     printf("Again?(1 to remove more, another key to exit):");
                                                     scanf("%d",&choice3);
                                                     }while(choice3 == 1);
                                                for (int j = 0 ; j < account_number ; j++)
                                                         fread(&temp2[j],sizeof(account),1,ptr);
                                                sort(temp2 , account_number);
                                                for(int k = 0 ; k < account_number ; k++)
                                                        fwrite(&temp2[k],sizeof(account),1,ptr);
                                                break;
                                         case 3:
                                                do{
                                                   if(!account_number){
                                                                       printf("Database is empty.\n");
                                                                       exit(1);
                                                                       }
                                                   printf("\nType the account's corresponding user name.\n");
                                                   account temp;
                                                   fgets(temp.user_name,MAX1,stdin);
                                                   change_password(&temp);
                                                   printf("Password changed.\n");
                                                   printf("again?(1 to change more passwords, any other key to exits):");
                                                   scanf("%d",&choice4);
                                                   }while(choice4 == 1);
                                         case 4 :
                                                do{
                                                   account temp;
                                                   printf("Enter the name of the account.\n");
                                                   fgets(temp.user_name,MAX2,stdin);
                                                   display(&temp,ptr);
                                                   printf("More details?(1 to confirm, else to exit):");
                                                   scanf("%d",&choice5);
                                                   }while(choice5 == 1);
                                         case 5:
                                                printf("You existed the program.\n");
                                                fclose(ptr);
                                                return 0; 
                                         default:
                                                 printf("Invalid Input.\n");
                                         }
                         }while(1);
                      }
               else{
                    printf("couldn't open the file.\n");
                    exit(1);
                    }
               printf("\n");
               system("PAUSE");
               return 0;
        }
    void display(account *x,FILE *ptr){
                                account temp;
                                strcpy(temp.user_name , "doesn't exist.");
                                strcpy(temp.password , "Doesn't exist.");
                                temp.privilige = 4;
                                int i = 0;
                                while(!feof(ptr)){
                                                  fread(&temp,sizeof(account),1,ptr);
                                                  if(!strcmp(temp.user_name , x->user_name)){
                                                                                            temp = *x;
                                                                                            exit(1);
                                                                                            }
                                                  }
                                printf("\tThe name is: %s\n",temp.user_name);
                                printf("\tThe password is: %s\n",temp.password);
                                switch(temp.privilige){
                                                       case 1 :
                                                              printf("\tThe privilige is admin.\n");
                                                              break;
                                                       case 2:
                                                              printf("\tThe privilige is: general\n");
                                                              break;
                                                       case 3:
                                                              printf("\tThe privilige is mail-only.\n");
                                                              break;
                                                       default :
                                                               printf("Doesn't exist.\n");
                                                       }
                                }
    
    
    account add_account(){
                          account x;
                          printf("\tEnter the user name:");
                          fgets(x.user_name,MAX2,stdin);
                          printf("\tenter the password");
                          int a = 0;
                          do{
                             fgets(x.password,MAX2,stdin);
                             int s = strlen(x.password);
                             for (int i = 0 ; i < s - 1 ; i++){
                                   if(!isalpha(x.password[i])){
                                                              a = 1;
                                                              break;
                                                              }
                                   }
                          }while(!a);
                          printf("\tEnter the privilige: (1 for admin, 2 for general, and 3 for mail-only)");
                          scanf("%d",&x.privilige);
                          while(x.privilige != 1 && x.privilige != 2 && x.privilige != 3){
                                  printf("Invalid privilige.Enter again:");
                                  scanf("%d",&x.privilige);
                                  }
                          return x;
            }
    void change_password(account *x){
                                    printf("Enter the passowrd for %s(should contain at least one non_alpha char)\n",x->user_name);
                                    int a = 0;
                                    char temp[MAX2];
                                    fgets(temp,MAX2,stdin);
                                    if (!strcmp(temp,x->password)){
                                                              printf("confirmation:");
                                                              fgets(temp,MAX2,stdin);
                                                              if (!strcmp(temp,x->password)){
                                                                                            printf("Ok, now enter the new password:");
                                                                                            do{
                                                                                               fgets(temp,MAX2,stdin);
                                                                                               int s = strlen(temp);
                                                                                               for (int i = 0 ; i < s - 1 ; i++){
                                                                                                   if(!isalpha(temp[i])){
                                                                                                                         a = 1;
                                                                                                                         break;
                                                                                                                         }
                                                                                                   }
                                                                                               }while(!a);
                                                                                            strcpy(x->password,temp);
                                                                                            printf("The password is now changed.\n");
                                                                                            }
                                                              else{
                                                                   printf("The entered password and the confirmation do not match each other.\n");
                                                                   }
                                                              }
          }
    void sort(account x[],int n){
                           int result;
                           int min;
                           for (int i = 0 ; i < n  - 1 ; i++){
                                 min = i;
                                 for (int j = i + 1 ; j < n; j++){
                                          if ((result = strcmp(x[i].user_name,x[j].user_name)) > 0)
                                                      min = j;
                                          }
                                 account temp;
                                 temp = x[i];
                                 x[i] = x[min];
                                 x[min] = temp;
                                 }
         }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning C4702: unreachable code
    By cpjust in forum C++ Programming
    Replies: 8
    Last Post: 05-05-2008, 03:24 PM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM