HELLO!
I am creating a student database with structs in C and I want to search for students by their names in the structure. I have tried strcmp function to compare the input name with student name in structs but its not working! My delete function is not working either! Can someone PLEASE HELP ME!
here is my code:
insert
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct personal_data{
int personal_num;
char name[20];
char gender[20];
char study_prg[20];
int age;
char email[20];
}personaData;
void add_student(personaData list[50], int x)
{
int i;
for(i = 0; i < x; i++)
{
printf("\nEnter data for student %d", i + 1);
fflush(stdin);
printf("\nName:");
gets(list[i].name);
printf("Gender:");
gets(list[i].gender);
printf("Study program:");
gets(list[i].study_prg);
printf("Email:");
gets(list[i].email);
printf("Personal number:");
scanf("%d", &list[i].personal_num);
printf("Age:");
scanf("%d", &list[i].age);
}
}
void modify_student(personaData list[50],int x, int pnum)
{
int i;
for(i = 0; i < x; i++)
{
if(list[i].personal_num == pnum)
{
printf("\nEnter data for student %d", i + 1);
fflush(stdin);
printf("\nName:");
gets(list[i].name);
printf("Gender:");
gets(list[i].gender);
printf("Study program:");
gets(list[i].study_prg);
printf("Email:");
gets(list[i].email);
printf("Age:");
scanf("%d", &list[i].age);
break;
}
else
printf("Student not found!");
}
}
void search_by_personalNum(personaData list[50], int x)
{
int pnum;
printf("Enter a personal number:");
scanf("%d", &pnum);
int i;
for(i = 0; i < x; i++)
{
if(list[i].personal_num == pnum)
{
printf("\nName: %s", list[i].name);
printf("\nGender: %s", list[i].gender);
printf("\nStudy program: %s", list[i].study_prg);
printf("\nEmail: %s", list[i].email);
printf("\nAge: %d", list[i].age);
break;
}
else
printf("Student not found!");
}
}
void search_by_Name(personaData list[50], int x)
{
char temp_name[20];
printf("\nEnter name: ");
fflush(stdin);
fgets(temp_name, sizeof(temp_name), stdin);
int i;
int isFound = 0;
for(i = 0; i < x; i++)
{
// if(list[i].name == temp_name)
// {
// isFound = 1;
// }
if(strcmp(list[i].name, temp_name) == 0)
{
isFound = 1;
break;
}
}
if(isFound == 1)
{
printf("\nPersonal number: %d", list[i].personal_num);
printf("\nGender: %s", list[i].gender);
printf("\nStudy program: %s", list[i].study_prg);
printf("\nEmail: %s", list[i].email);
printf("\nAge: %d", list[i].age);
}
else
printf("Student not found!");
}
void Delete(personaData list[50], int x)
{
int pnum;
printf("Enter a personal number:");
scanf("%d", pnum);
char emptyStr[20] = {"\0"};
int emptyStr1[20] = {0};
int i;
for(i = 0; i < x; i++)
{
if(list[i].personal_num == pnum)
{
strcpy(emptyStr, list[i].name);
strcpy(emptyStr, list[i].gender);
strcpy(emptyStr, list[i].email);
strcpy(emptyStr, list[i].study_prg);
strcpy(emptyStr1, list[i].age);
strcpy( emptyStr1, list[i].personal_num);
}
printf("Not found");
}
}
int main()
{
personaData students[20];
int n;
char Name[20];
printf("Number of records you want to enter? : ");
scanf("%d", &n);
add_student(students, n);
Delete(students, n);
search_by_Name(students, n);
return 0;
}