Hi my program isn't work well. There's a little problem. Maybe someone could help me? My gets(name) is not working. program always pass the gets line after executing it. I couldn't enter anything to name variable. And also this problem is only happening in a loop? What's the problem you think?
Here is the program
Code:
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
main()
{
char name[30], sex;
int n, counter, age;
double gpa;
FILE *inp, *outp;
inp = fopen("stdlist.txt", "r");
outp = fopen("stdlist.dat", "w");
printf("How many students: ");
scanf("%d", &counter);
n=1;
while(n<counter)
{
puts("Please enter student's name:");
gets(name);
printf("Please enter student's sex (M or F): ");
scanf("%c", &sex);
printf("Please enter student's age: ");
scanf("%d", &age);
printf("Please enter student's GPA: ");
scanf("%lf", &gpa);
system("cls");
printf("\n\n----------------------------------------\n\n");
printf("This student's information is \nname: %s\nsex: %c\nage: %d\nGPA: %lf\n", name, sex, age, gpa);
printf("\n------------------------------------------\n\n");
printf("Please press a key for print these values to output file.\n");
getch();
fprintf(outp, "----------------------------------------------\nName: %s\nSex: %c\nAge: %d\nGPA: %0.2lf\n-----------------------------------------------\n\n", name, sex, age, gpa);
n++;
}
return 0;
}