Need help with basic program in C
I have a menu when i select 2 variables and then i must choose between a man and a woman. After that i must go to man.c or woman.c with the 2 variables previously choosed but i dont know how can i do that.
my main.c (only when the man option in the menu):
printf("Insert weight: ");
scanf("%f",&a);
printf("Insert high: ");
scanf("%f",&b);
switch(opcion){
case 'm':;
--here i want to go to man.c to continue with other menu but knowing variables weight and high--
man.c and woman.c are similars the ionly differen开发者_如何学Goce is when calculates the body mass index
man.c :
int bmi(float weight, float high){
float bmi;
char opcion;
printf("a) Calculate body mass index");
switch(opcion){
case 'a': bmi = weight / high;
break;
}
}
now i ave only this and woman is the same. When is finished man.c and woman.c will have 4 options using weigh, high and some variables more that they asked when needed with scanf.
I'd suggest you call a function (say manMenu()
) and keep it in same .c file.
1) You can't simply navigate through c files in C.
2) You can do that using includes & classes, but it's a bit hard for a beginner
3) The right way to do it is something like this:
printf("M/F");
scanf("%f",&option);
switch(option){
case M:
do_man();
break;
case F:
do_woman();
break;
}
And you should declare the functions do_man() and do_woman() before the main.
General:
- It's mistake about thinking about code in means of filenames. Instead you should think about functions.
- add error handling to the menu e.g. verify input and repeat in a loop until correct input or escape char.
Solution.
Add two functions void handleMan(float weight ,float height);
and void handleWoman(float weight ,float height);
prototypes to main.c (just copy code before menu()
or main()
and implement them in man.c and woman.c later on call right method upon user selection.
The easiest way would be to call a gender specific function in the switch. For example man_menu() and woman_menu(). These could also be located in different .c files but then you need to link the object files together.
Well you have to define procedures for man and woman and call procedure in switch statement and perform your individual activities in the respective methods .
精彩评论