Results 1 to 4 of 4

Thread: Switch statement problem

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Switch statement problem

    Hello

    The program will ask the user to select from the menu options, then call that function.

    Can you tell me is there a better way to write this switch statement. My programming does not work well, and l am wondering is there a better way to write this type of program.

    One more quick question, what is the keyword that allows you to clear the screen, so when a menu option is selected it will display a new screen.

    My code is below

    Thanks in advance

    VB Code:
    1. #include "stdio.h"
    2. #include "ctype.h"
    3. #include "conio.h"
    4.  
    5. //Function prototypes
    6. void Divide(void);
    7. void Multiply(void);
    8. void Subtrack(void);
    9. void Add(void);
    10.  
    11. void main(void)
    12. {
    13.     int MenuOption = 0;
    14.     char Exit = NULL;
    15.  
    16.     printf("Welcome to Mr Calculator");
    17.     printf("\n[1] Add");
    18.     printf("\n[2] Subtract numbers");
    19.     printf("\n[3] Divide numbers");
    20.     printf("\n[4] Multiply numbers");
    21.     printf("\n[5] Exit from program");
    22.  
    23.     do{
    24.         printf("\nPlease choose a option from the menu: ");
    25.         scanf("%d",&MenuOption);
    26.  
    27.         switch(MenuOption)
    28.         {
    29.             case(1):
    30.                 //Function to add
    31.                 break;
    32.             case(2):
    33.                 //Function to subtract
    34.                 break;
    35.             case(3):
    36.                 //Function to divide
    37.                 break;
    38.             case(4):
    39.                 //Function to multiply
    40.                 break;
    41.             case(5):
    42.                 printf("Are you sure you want to exit this program (y/n): ");
    43.                 Exit = getchar();
    44.                 Exit = toupper(Exit);
    45.             default:
    46.                 printf("Invalid - input, please re-enter");
    47.         }
    48.     }while(MenuOption != 'Y');
    49. }
    50.  
    51. void Add(void)
    52. {
    53.     //Code to go here
    54. }
    55.  
    56. void Subtract(void)
    57. {
    58.     //Code to go here
    59. }
    60.  
    61. void Multiply(void )
    62. {
    63.     //Code to go here
    64. }
    65.  
    66. void divide(void )
    67. {
    68.     //Code to go here
    69. }
    steve

  2. #2
    New Member
    Join Date
    Oct 2002
    Posts
    6
    well steve,

    you can use the clrscr(); and getch(); to clear your display screen scien you have the #include<conio.h> example:

    #include<conio.h>
    //==============
    prototype
    //===============
    void main(){
    clrscr();
    printf("Enter name:");
    scanf(" %s",&name);
    printf("Enter age:");
    scanf(" %d",&age); //call function here
    //print result here
    getch();
    }
    //===============
    sub function here
    *********************************
    for your switch()
    you try to change the "case (1):" to "case '1' " I don't think it is correct to put (1) may be is '1

    you also try to put the do..while loop where do is above the main menu.

    do{
    printf("Welcome to Mr Calculator");
    printf("\n[1] Add");
    printf("\n[2] Subtract numbers");
    printf("\n[3] Divide numbers");
    printf("\n[4] Multiply numbers");
    printf("\n[5] Exit from program");

  3. #3
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    I don't see how you exit the do.. while loop
    Try this:
    Code:
      do{
    	printf("\nPlease choose a option from the menu: ");
    	scanf("%d",&MenuOption);
    	switch(MenuOption)
    	{
    		case(1):
    			//Function to add
    			break;
    		case(2):
    			//Function to subtract
    			break;
    		case(3):
    			//Function to divide
    			break;
    		case(4):
    			//Function to multiply
    			break;
    		case(5):
    		        for(Exit=0x00;Exit=='N' || Exit == 'Y' ; ){
    			  printf("Are you sure you want to exit this program (y/n): ") ;
    			  Exit = getchar();
    			  Exit = toupper(Exit);
    			}
    			if(Exit=='Y') MenuOption=0;
    			break;
    		default:
    			printf("Invalid - input, please re-enter");
    	}
      }while(MenuOption);
    And - there is nothing wrong with using switch() - it's the best choice in this situation.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    clrscr() is a borland extension. Use system("cls").
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width