doing a hw assignment using a structure. i have to create a db of students. ask user to determine how many students (max 10) to enter. my problem is my code works ok, but im overwriting my data everytime the user enters new info for each student. so when i print out the info, it only prints the last entry the user inputed. i know this is something fairly simple but i just cant get it, any ideas? here is my code:
Code:
#include <stdio.h>
struct student
{
char name[20];
char city[20];
int id;
float units;
char year[10]; //freshman, sophomore, junior, senior
}x;
int main()
{
int i, num;
printf("Enter number of students to input in database: ");
scanf("%d", &num);
for(i = 0; i < num; i++)
{
printf("Enter students name:\n");
scanf("%s", x.name);
printf("Enter students city:\n");
scanf("%s", x.city);
printf("Enter students I.D.:\n");
scanf("%d", &x.id);
printf("Enter number of units:\n");
scanf("%f", &x.units);
printf("Enter year of student:\n");
scanf("%s", x.year);
}
printf("%s\n", x.name);
printf("%s\n", x.city);
printf("%d\n", x.id);
printf("%g\n", x.units);
printf("%s\n", x.year);
return 0;
}