For some reason, when I insert data to the student struct, the data that I inserted is not saved.
If I print st[]
after each run, the data that I inserted in scanf()
is not beign saved.
What should I do so that add_rec()
will save my data?
my code is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char stnumber[10];
char stname[20];
char gender;
float quiz1;
float quiz2;
float assigment;
float midterm;
float final;
float total;
}student;
/*
Defining the add_rec(student[] st, int *itemcount) function to add a new record to the the array of student objects.
This function takes two arguments.
The first argument is the array of student objects(st) and the second argument is the number of items in the array.
This function firstly checks the new record(using the search function that is defined in the next step) before it is allowed to be appended to the array to avoid duplicate records.
When the new item is added the value of itemcount variable increases by 1 that means the number of records in the list increases.
*/
void add_rec(student st[],int *itemcount)
{
againID:
printf("\nEnter the student's ID (9 digits): ");
scanf(" %s",&st[*itemcount].stnumber);
if (strlen(st[*itemcount].stnumber) != 9)
{
printf("invalid\n"); goto againID;
}
printf("Enter the student's Name: ");
scanf(" %s",&st[*itemcount].stname);
againGender:
printf("Enter the student's Gender(F or M): ");
scanf(" %c",&st[*itemcount].gender);
if (st[*itemcount].gender != 'm' && st[*itemcount].gender != 'M' && st[*itemcount].gender != 'f' && st[*itemcount].gender != 'F')
{
printf("invalid\n"); goto againGender;
}
againquiz1:
printf("Enter the student's 1st quiz score: ");
scanf(" %f",&st[*itemcount].quiz1);
if (st[*itemcount].quiz1 < 0 || st[*itemcount].quiz1 > 100)
{
printf("invalid\n"); goto againquiz1;
}
againquiz2:
printf("Enter the student's 2nd quiz score: ");scanf(" %f",&st[*itemcount].quiz2);
if (st[*itemcount].quiz2 < 0 || st[*itemcount].quiz2 > 100)
{
printf("invalid\n"); goto againquiz2;
}
againAssigment:
printf("Enter the student's assigment score: ");scanf(" %f",&st[*itemcount].assigment);
if (st[*itemcount].assigment < 0 || st[*itemcount].assigment > 100)
{
printf("invalid\n"); goto againAssigment;
}
againMidterm:
printf("Enter the student's mid-term score: ");scanf(" %f",&st[*itemcount].midterm);
if (st[*itemcount].midterm < 0 || st[*itemcount].midterm > 100)
{
printf("invalid\n"); goto againMidterm;
}
againFinal:
printf("Enter the student's final score: ");scanf(" %f",&st[*itemcount].final);
if (st[*itemcount].final < 0 || st[*itemcount].final > 100)
{
printf("invalid\n"); goto againFinal;
}
st[*itemcount].total = st[*itemcount].quiz1 + st[*itemcount].quiz2 + st[*itemcount].assigment + st[*itemcount].midterm + st[*itemcount].final;
++(*itemcount);
}
int main()
{
student st[20];
int itemcount=0;
char confirm;
do
{
add_rec(st, &itemcount);
printf("Press y or Y to continue: ");
scanf("%s",&confirm);
} while(confirm=='y'||confirm=='Y');
return 0;
}
Thanks to all the helpers :)