I am writing a simple program in c to better understand the language, but I have a strange problem. As the code below shows, I only have one loop that it completes when I insert 255 as the value. The problem is that when I select the first (insertion option), and after I insert the name, the program starts something like a loop and gives me the selection screen all the time ...
#include<stdio.h>
#include<stdlib.h>
struct student{
char *name;
int id;
};
void insertStudent(void);
struct student * init(void);
int main(){
struct student *p;
int selectionCode=0;
while(selectionCode!=255){
printf("\nInsert students:1");
printf("\nDisplay students:2");
printf("\nExit:255");
printf("\n\nEnter selection:");
scanf("%d",&selectionCode);
p=init();
switch(selectionCode){
case 1:
insertStudent();
break;
case 2:
break;
case 255:
break;
}
}
return 0;
}
struct student *init(void)
{
struct student *p;
p=(struct student *)malloc(sizeof(struct student));
return p;
}
void insertStudent(void){
struct student *p;
p=init();
printf("Enter Name:");
scanf("%s",p->name);
printf("Enter ID:");
scanf("%d",&p->id);
}
source
share