I have a problem that when the users type same id,they need to type name and id again. How can i only ask the users to input the id rather than both name and id,when the users type same id number.Please give me some hints how to do that.Thanks for your help!
struct student
{
char student_name[30];
char student_id[10];
int student_course_num[20];
int student_course[10];
};
int main()
{
int student_num;
printf("Enter the number of students:");
scanf("%d",&student_num);
fflush(stdin);
struct student S[student_num];
char TestForId[student_num][10];
int i,j,student_code=1;
for(i=0;i<student_num;i++)
{
printf("Enter the name of student:");
fgets(S[i].student_name,30,stdin);
printf("Enter the Student ID (8 digits):");
fgets(S[i].student_id,10,stdin);
strcpy(TestForId[i],S[i].student_id);
for(j=0;j<i;j++)
{
if(strcmp(TestForId[j],S[i].student_id)==0)
{
printf("The student id has already exit\n");
}
}
student_code++;
}
fflush(stdin);
is undefined. And with your program it's not needed.fflush(stdin)
is technically defined by Microsoft. In fact, I learned last week, the same is true on Linux (For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application). This applies to Mac OS X too. The C standard leaves the behaviour undefined; so does POSIX. But on 2 key platforms and one important platform,fflush(stdin)
is defined and useful behaviour.student_id[30]
? How many IDs can one student have?