How do you clean out the contents of an array after you take input?
In my program, I put fgets in a loop to get input. However, once I loop back to the beginning to get input for the second time, the previous input values are still in the array, so it skips the fgets :cry:
The problem occurs in the first while loop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 20
#define CSIZE 4
struct name {
char first[SIZE];
char last[SIZE];
};
struct student {
struct name ppl;
float grade[3];
float avg;
};
int main(void)
{
static struct student data[CSIZE] = {
{{"Bill", "Gates"}, {0.0, 0.0, 0.0}, 0.0},
{{"Mace", "Windu"}, {0.0, 0.0, 0.0}, 0.0},
{{"Darth", "Vader"}, {0.0, 0.0, 0.0}, 0.0},
{{"Happy", "Dog"}, {0.0, 0.0, 0.0}, 0.0},
};
char inputA[SIZE], inputB[SIZE];
int i = 0, j = 0, k = 0;
float score = 0, sum = 0;
while (i < CSIZE)
{
fputs("First name:", stdout);
fgets(inputA, SIZE, stdin);
fputs("Last name:", stdout);
fgets(inputB, SIZE, stdin);
while (j < 3)
{
printf("score %d: ", j+1);
scanf("%f", &score);
data[i].grade[j] = score;
sum += score;
j++;
}
data[i].avg = (sum / 3);
j = 0;
i++;
}
while (k < CSIZE)
{
printf("%s %s. Scores = %.2f %.2f %.2f. Average = %.2f\n", data[k].ppl.first, data[k].ppl.last,
data[k].grade[0], data[k].grade[1], data[k].grade[2], data[k].avg);
k++;
}
return 0;
}