Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define ADD "add"
#define PRINT "print"
struct info
{
char name[50];
char age[50];
struct info *next;
};
struct info * allocate(void);
void get_elements(struct info *t);
int main()
{
struct info *root;
struct info *current;
char *command;
int x=0;
root=NULL;
while(1)
{
printf("Enter Command:");
scanf("%s",command); // You should use fgets, u can avoid using getchar after scanf
getchar();
if( strcmp(command,ADD)==0 )
{
if(root==NULL)
{
root=allocate();
get_elements(root);
current=root;
}
else
{
current->next=allocate();
get_elements(current->next);
current=current->next;
}
}
else if(strcmp(command,PRINT)==0)
{
if(root==NULL)
{
fprintf(stderr,"There Is No List\n");
exit(1);
}
else
{
current=root;
printf("---------------------------------------\n");
printf("Recorde\t\tName\t\tAge\n");
printf("--------------------------------------\n");
while(current!=NULL)
{
x++;
printf("%d\t\t%s\t\t%s\n",x,current->name,current->age);
current=current->next;
}
break;
}
}
else
printf("Undefined Command %s\n",command);
}
getchar();
return 0;
}
struct info * allocate(void)
{
return malloc (sizeof struct info);
}
void get_elements(struct info *t)
{
printf("Enter Your Name:");
scanf("%s",t->name); // fgets(t->name,sizeof t->name, stdin);
getchar();
printf("Enter Your Age:");
scanf("%s",t->age); // fgets(t->age,sizeof t->age, stdin)
getchar();
}
You indendation should be good, to be a good programmer. And look at the comments along the code.
ssharish2005