Sunday, July 12, 2009

Please tell me whats wrong with the C header file i created ?

i am trying to creat a C header file. i want a funcion that prints a menu, prompt for the choice, scan it and return it to function main. so i created header file and tried to use the function in a program but not working pls tell me whats wrong here.





header file................





int menu_bread()


{


int choice;


printf("1.Baguette : 2.Foccacia : 3.Ciabatte");


printf("\nbread type:");


scanf("%d",choice);


return choice;


}








main function i ivoked the function ............





#include%26lt;stdio.h%26gt;


#include%26lt;program.h%26gt;





main()


{


int choice


choice=menu_bread();


if(choice==1)


printf("ok");


}











program.h is the header file i created.

Please tell me whats wrong with the C header file i created ?
I understand you are trying to do some thing. I dont want to go deap into this. All the best!





The error is due to the missing '%26amp;' before the 'choice' in the "scanf("%d",choice);" statement in the menu_bread() function.





Also make sure you place your header file (say) in the correct path. First try to have the function in the main *.c file and test. Then you move that as a seperate file. You will be able to debug easily. Hope this helps.





Please note only when you press '1' you will get the output in the console or else noting will be there. So you may like to add an else statement saying something to make sure it works or not. Please add a getche(); or getch() at the end of the program too.
Reply:don't forget that the header file always just contains the declaration of the function, not the function body:





header file:





extern int menu_bread();





C file:


int menu_bread()


{


...


}





(in your case, it also works if your header file contains the function body, but this is an exception and absolutely not recommended because it will give you a lot of trouble in bigger projects).


No comments:

Post a Comment