5 Replies - 293 Views - Last Post: 26 June 2011 - 10:55 PM Rate Topic: -----

#1 roachmmflhyr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 29-January 11

Control what case is displayed in Switch statement with if statement

Posted 26 June 2011 - 10:04 PM

Is it possible to control what case's are used in a switch statement with an if statement? For instance:

      switch(menuSelection){
         case 1:
            printf("Please type a file name you want to select: ");
			// Get user input for file name
            scanf("%s", userInput);
            printf("You selected %s",userInput);				
			// Open file in a buffer
			if(userInput){
				fp = fopen(userInput, mode);
				fileSet = false;
			}
            printf("\n\n");
            break;

         if(fileSet != false){
            case 2:
               printf("You have reached case 2\n");
               break;
            case 3:
               break;
            case 4:
               break;
            case 5:
               break;
            case 6:
               break;
            case 7:
               break;
         } else {
			default: printf("Sorry only case 1 is available");
		 }



If file name wasn't selected then cases 2 through 7 would not become available.....

Is This A Good Question/Topic? 0
  • +

Replies To: Control what case is displayed in Switch statement with if statement

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,432
  • Joined: 25-December 09

Re: Control what case is displayed in Switch statement with if statement

Posted 26 June 2011 - 10:16 PM

NO. If 1 is selected then 2 - 7 will not execute.

Jim

This post has been edited by jimblumberg: 26 June 2011 - 10:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 roachmmflhyr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 29-January 11

Re: Control what case is displayed in Switch statement with if statement

Posted 26 June 2011 - 10:40 PM

View Postjimblumberg, on 26 June 2011 - 10:16 PM, said:

NO. If 1 is selected then 2 - 7 will not execute.

Jim


Well what if I didn't want 2-7 to execute unless a file was picked in case 1 first?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5248
  • View blog
  • Posts: 27,071
  • Joined: 10-May 07

Re: Control what case is displayed in Switch statement with if statement

Posted 26 June 2011 - 10:52 PM

** untested **


int switch_ch=0;

       switch(menuSelection){
         case 1:
            if(switch_ch==0) {
               printf("Some horrible break message...\n");
               break;
            }
            printf("Please type a file name you want to select: ");
			// Get user input for file name
            scanf("%s", userInput);
            printf("You selected %s",userInput);				
			// Open file in a buffer
			if(userInput){
				fp = fopen(userInput, mode);
				fileSet = false;
			}
            printf("\n\n");
            if(fp) switch_ch++;
            break;

         if(fileSet != false){
            case 2:
              if(switch_ch==0) break;
               printf("You have reached case 2\n");
               break;
            case 3:
              if(switch_ch==0) break;
               break;
            case 4:
              if(switch_ch==0) break;
               break;
            case 5:
              if(switch_ch==0) break;
               break;
            case 6:
              if(switch_ch==0) break;
               break;
            case 7:
              if(switch_ch==0) break;
               break;
         } else {
			default: printf("Sorry only case 1 is available");
		 }



Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1657
  • View blog
  • Posts: 3,136
  • Joined: 30-May 10

Re: Control what case is displayed in Switch statement with if statement

Posted 26 June 2011 - 10:54 PM

Perhaps it would be better written as
if ( menuSelection == 1 ) {
  // do file open stuff
} else if ( fileSet ) {
  switch ( menuSelection ) {
    case 2:
...
    case n:
  }
}


Was This Post Helpful? 1
  • +
  • -

#6 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5248
  • View blog
  • Posts: 27,071
  • Joined: 10-May 07

Re: Control what case is displayed in Switch statement with if statement

Posted 26 June 2011 - 10:55 PM

Or you could just check for the file pointer success before handling any file items.

Test values
Process
Validate output
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1