-
fgets issue
Code:
#include<stdio.h>
#include<string.h>
void avg_min (double *, int *, FILE *);
int main (void)
{
double avg, preavg, postavg, quizavg;
int min, premin, postmin, quizmin, check;
double finalgrade;
int choice = 1;
int counter= 0;
FILE *out;
char student_name[30]={""};
char *inputptr;
out = fopen("results.txt","a");
while (choice == 1)
{
printf("1. Next student\n");
printf("2. Done\n");
printf("Enter choice:");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("Enter the student's name:");
inputptr = fgets (student_name, 30, stdin);
printf( inputptr );
if( inputptr != NULL)
{
printf( inputptr );
//fputs (student_name, out);
}
in case 1, when fgets() is called it does not wait for user input and continues on to next lines of code. I'm not sure why it is doing this.
-
This
Code:
scanf("%d",&choice);
is leaving the newline character in the input buffer.
-