I made a menu program that has 5 menus: add, delete, insert and display record and exit. The program has an array of 20 that is to be filled with unique integers by the user.
Here's my program:
#include<stdio.h>
int array[20];
int dcn;
int cntr=0;
add()
{
printf("Please enter the value: ");
scanf("%d",&array[cntr]);
cntr++;
printf("1-Add More/2-Return to Menu");
scanf("%d",&dcn);
if(dcn==1)
{
while(cntr<20)
{
add();
}
printf("No space for new!");
return main();
}
else
{
return main();
}
}
del()
{
int seg, jr, *p;
p = &array[cntr];
printf("Enter the value you want to delete: ");
scanf("%d",&seg);
for(jr=0;jr<20;jr++)
if(*p ==seg)
{
*p = *p+1;
}
printf("1-Delete More/2-Return to Menu");
scanf("%d",&dcn);
if(dcn==1)
{
while(*p = '\0')
{
del();
}
printf("No value to delete!");
return main();
}
else
{
return main();
}
}
insert()
{
int jun, ong, don, *gin;
gin = &array[cntr];
printf("Enter the value where you want to insert: ");
scanf("%d",&jun);
printf("Enter the vaue you want to insert: ");
scanf("%d",&don);
for(ong=0; ong<20; ong++)
if(*gin==jun)
{
*gin=don;
}
return main();
}
display()
{
int rod;
printf("Your array elements are as follows:\n\n");
for(rod=0;rod<20;rod++);
{printf("%d\t",&array[cntr]);
cntr++;}
main();
}
}
I don't know where my program went wrong. I'm only a student and I think there's much better than this. Please help, thanks! I'd tried to encode this in turbo ver.3 and dev c++ but still I don't get it right.
Hi, please can you edit your post to enclose the code in tags (but use lowercase c) and provide indentation - it is difficult to quickly analyse unformated code:-)
Details on how the program is failing would also help.
The one thign I did spot at a glance is you have not given function types on the function definitions, and you havereturn main(); where you want return;
There are some problems in your program, please correct them.
1) Please check the main function
It contains following lines of code
"scanf("%d",&dcn);
if (num==1)
{"
You are getting the value in dcn variable and applying if condition in num variable, so it will exit without doing anything.
So change num to dcn.
2) Second problem: You are calling main function from other functions like add etc. This is not a good way to display a menu. It will cause stack currupt at some point of time due to following reason.
main ---> add --> main --> add and so on.
In this way you will load main variables in stack n number of time. So your program will crash. Please check it and think something else.