Reputation: 19
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++;
}
Upvotes: 0
Views: 399
Reputation: 153338
Create an array of struct student
, not just a single entry. struct student S[Student_N];
Create a count of used records. size_t Student_Count = 0;
Ask the student ID first before the student name.
Read the student ID ( and later student name) into a local struct student local;
. scanf("%29s", local.student_id);
Use a width of sizeof(local.student_id) - 1
.
Before asking the student name, search your list 0
up to Student_Count
for a matching entry. If found, fill in the rest of local with the matching data, skip next 2 steps.
Read the student name into a local struct student local;
. scanf(" %49[^\n]", local.student_name);
. Use a format specifier that scans in spaces between first & last name.
Copy local
to student_id[Student_Count++]
.
Not sure you need the student_num
field. The index of S[Student_N]
is the student_num
.
Check the results if scanf()
as in if (scanf("%29s") != 1) Handle_Error();
.
Delete fflush(stdin);
Upvotes: 1
Reputation: 1271
I would create a struct array where each element is a struct containing ID and name. Then I would keep the array IDs sorted, so that it would really easy to check if the ID exists.
e.g:
new ID=10
the biggest ID in the array is 7 (this information is known because
the array is sorted) therefore no need to check further, just add a
new ID
In order to do so , you could use qsort
and bsearch
Upvotes: 0
Reputation: 2729
Try this:
for(i=0;i<StuNum;i++)
{
printf("Number of Students %d:\n",student_code);
printf("Enter the name of student:");
scanf("%s",&S.student_name[i]);
fflush(stdin);
printf("Enter the Student ID :");
scanf("%d",&S.student_id[i]);
fflush(stdin);
for(j=0;j<i;j++)
{
if(S.student_id[j]==S.student_id[i]) //whether the id is same or not
{
printf("<ID NUMBER HAS ALREADY EXITED>\nEnter the ID again: ");
scanf("%d",&S.student_id[i]);
break;
}
}
student_code++; }
Upvotes: 1
Reputation: 6070
your problem is: you have one struct S which is supposed to hold all values. But inside S you have only one Array for the name, instead of an array of char-arrays for all names.
Maybe what you want is
struct student {
char name[50];
int id;
}
struct student sarray[30];
Upvotes: 1
Reputation: 1305
You have to use strcmp
instead of if(S.student_id[j]==S.student_id[i])
.
Something like :
if(0 == strcmp(S.student_id[j],S.student_id[i]))
More info about strcmp : Here
Also id should not be an int array but a char array (string right ?)
I don't really like your int StuNum=S.student_num;
it should be at the top of the main.
Upvotes: 0