PDA

Click to See Complete Forum and Search --> : Switch statement problem


steve_rm
Oct 24th, 2002, 09:10 PM
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



#include "stdio.h"
#include "ctype.h"
#include "conio.h"

//Function prototypes
void Divide(void);
void Multiply(void);
void Subtrack(void);
void Add(void);

void main(void)
{
int MenuOption = 0;
char Exit = NULL;

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");

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):
printf("Are you sure you want to exit this program (y/n): ");
Exit = getchar();
Exit = toupper(Exit);
default:
printf("Invalid - input, please re-enter");
}
}while(MenuOption != 'Y');
}

void Add(void)
{
//Code to go here
}

void Subtract(void)
{
//Code to go here
}

void Multiply(void )
{
//Code to go here
}

void divide(void )
{
//Code to go here
}

xxx
Oct 25th, 2002, 01:27 AM
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");

jim mcnamara
Oct 25th, 2002, 09:17 AM
I don't see how you exit the do.. while loop
Try this:

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.

CornedBee
Oct 25th, 2002, 11:01 AM
clrscr() is a borland extension. Use system("cls").