hi guys,im trying to create a menu which allow user to select their choice. When a choice is selected, a function designed for that choice will be called. However,it seems that i cant use 'if else' to call them, is there anyway to solve it?im sure i did it wrongly...if you cant understand what im talking about, here is the thing im doing : https://docs.google.com/document/d/1TsQJf0vlmkBNkwvt6rsKCAVshKyTNkm10YEbmIVeNyg/edit
thanks for taking time ! ^^

#include <stdio.h>


//Function Declarations
void quiz (void);
void calculator (void);
void optional (void);




int main (void)



{	

//Local Declarations
	int menu;
	

//Statements
	printf("\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
	printf("\t\t@   WELCOME TO CHEMY'S CHEMISTRY   @\n");
	printf("\t\t@                                  @\n");
	printf("\t\t@            MAIN MENU             @\n");
	printf("\t\t@                                  @\n");
	printf("\t\t@   1. CHEMISTRY QUIZ              @\n");
	printf("\t\t@   2. IDEAL GAS LAW CALCULATOR    @\n");
	printf("\t\t@   3. OPTIONAL GAME               @\n");
	printf("\t\t@                                  @\n");
	printf("\t\t@   0. EXIT                        @\n");
	printf("\t\t@                                  @\n");
	printf("\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n");
	printf("Please choose your menu: ");
	scanf("%d", &menu);

	if (menu == 1)
		quiz ();

	else if (menu == 2)
		calculator ();
	else if (menu == 3)
		optional ();

	else
		printf("I'm sorry, but you have enter an invalid selection. Please try again.");
	return 0;

}

Recommended Answers

All 6 Replies

However,it seems that i cant use 'if else' to call them

Could you elaborate on this? The only real problem with how you're calling the functions is that they don't exist; you've only declared the function signatures, not actually defined the function bodies.

omg >.< i thought i could declare and call them without define them first,now its solved >.< thanks a lot! sorry for the silly question!im still new at this, been spending like 2 hours google and asking around,thank you for ur help!

you can use switch case condition to replace the if else in this case

switch(menu)
{
case 1: quiz();

case 2: calculator();
case 3: opitional();
default:  Printf("I'm sorry, but you have enter an invalid selection. Please try again.");
}

[URL snipped]

ok thanks!btw,if i use switch,how do i repeat it so that in default,it will ask user to input again?

use a loop where the condition is while the user doesn't enter a specific number it will continue to loop and insert it around the user input and switch case

example:

while(menu != 4){
printf("Please choose your menu: ");
	scanf("%d", &menu);
switch(menu)
{
case 1: quiz();
        break; //don't forget to use break otherwise it will ran through other cases
case 2: calculator();
        break;
case 3: optional();
        break;
case 4: printf("Goodbye\n");
        break;
default: printf("I'm sorry, but you have enter an invalid selection. Please try again.");
        break;
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.