HMm... already frustrated at it... But is there something wrong in my malloc? Everytime I try to display the record, it gives me random things as answer(see code below)? Was it that I didn't free it or was I wrong in declaring malloc itself?
Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
typedef struct{ //Structure for student profile
char fname[100];
char lname[100];
char college[50];
char course[50];
int batch; //Year Entered UP.
int snum; //Student Number
int age;
float gpa;
}student;
int search_snum();
void display_All();
void edit();
void menu();
main(){
student s;
int x; //Choices for the menu, x for the counter-controlled events
int size, choice, choice2; //"size" Deterrmines number of students for the memory allocation
int id;
student *s_record; //Record's array, to be created using malloc.
system("cls");
printf("Enter Number of Students: ");
scanf("%d", &size);
s_record = malloc(sizeof(student) * size);
do{
printf("Menu: \n\n1. Edit Student Information\n2. Search for a student\n3. Display All Students\n4. Quit\nChoice: ");
scanf("%d",&choice);
system("cls");
switch( choice){
case 1:
for( x = 0 ; x < size; x++ ){
edit(x, s_record);
}
system("cls");
printf("Filling of Student Information Complete.\n\n");
break;
case 2:
id = search_snum(s_record, size);
system("cls");
printf("Student Information:\n\n");
printf("Student Number: %d-%d\n",s_record[id].batch,s_record[id].snum);
printf("Name: %s, %s\n",s_record[id].lname, s_record[id].fname);
printf("College: %s\n",s_record[id].college);
printf("Course: %s\n",s_record[id].course);
printf("Age: %d\n",s_record[id].age);
printf("GPA: %.2f\n",s_record[id].gpa);
putchar('\n');
break;
case 3:
printf("Students:\n\n" );
for(x=0 ; x<size ; x++ ){
printf("%3d: %4d %6d : %s, %6s %5s %s %.2f\n", x+1, s_record[x].batch, s_record[x].snum, s_record[x].lname, s_record[x].fname, s_record[x].college, s_record[x].course, s_record[x].gpa);
}
putchar('\n');
break;
case 4:
break;
}
}while(choice!=4);
free(s_record);
}
void edit(int x, student *s_record){
system("cls");
printf("Student Information:\n\n");
printf("Enter Year Entered in the University: ");
scanf("%d", &s_record[x].batch);
printf("Enter Student Number: ");
scanf("%d", &s_record[x].snum);
printf("Enter Last Name: ");
scanf("%s", &s_record[x].lname);
printf("Enter Name First Name: ");
scanf("%s", &s_record[x].fname);
printf("Enter College: ");
scanf("%s", &s_record[x].college);
printf("Enter Course: ");
scanf("%s", &s_record[x].course);
printf("Enter Age: ");
scanf("%d", &s_record[x].age);
printf("Enter GPA: ");
scanf("%f", &s_record[x].gpa);
}
int search_snum(student *s_record, int size){
int ptr=0;
int id;
printf("Enter Student Number, without the 'year entered in the university' : ");
scanf("%d",&id);
while (ptr<size){
if (s_record[ptr].snum != id){
ptr++;
}else{
return ptr;
}
}
}