|
-
Oct 24th, 2002, 09:10 PM
#1
Thread Starter
Frenzied Member
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:
#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
}
-
Oct 25th, 2002, 01:27 AM
#2
New Member
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");
-
Oct 25th, 2002, 09:17 AM
#3
Frenzied Member
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.
-
Oct 25th, 2002, 11:01 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|