0

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++;
}   
8
  • 4
    Technically, fflush(stdin); is undefined. And with your program it's not needed. Commented Dec 2, 2013 at 15:40
  • 1
    As for your problem, have you tried a loop around the input for the id and the check? Commented Dec 2, 2013 at 15:42
  • why the int-array for student_id? Commented Dec 2, 2013 at 15:44
  • 2
    @JoachimPileborg: On Windows, 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. Commented Dec 2, 2013 at 15:48
  • why student_id[30]? How many IDs can one student have? Commented Dec 2, 2013 at 16:02

5 Answers 5

1

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];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you!In fact,the original char name defined by two arrays by my teacher.I have been think why the the name have two arrays.It is confused me so i modify to one array.
1

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++; }   

3 Comments

"Number of students" asked every time inside the "for i"-loop?
Only when the ID matches, ask the user to input the ID again with the same ith value...
i tried this ,when user type the same value it can't define whether the id is same or not
1
  1. Create an array of struct student, not just a single entry. struct student S[Student_N];

  2. Create a count of used records. size_t Student_Count = 0;

  3. Ask the student ID first before the student name.

  4. 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.

  5. 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.

  6. 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.

  7. Copy local to student_id[Student_Count++].

  8. Not sure you need the student_num field. The index of S[Student_N] is the student_num.

  9. Check the results if scanf() as in if (scanf("%29s") != 1) Handle_Error();.

  10. Delete fflush(stdin);

2 Comments

Thanks for your detailed instruction.I want to ask a question.because my project has a lot of requirements.I have to follow.if i type the name first.How can i only check the student id?
@user3041923 Assume the first input is a name or an ID. If the text entered matches an earlier ID, then its an ID. If the text looks like an ID (it has digits in it and no spaces), then it is an ID (or whatever your criteria may be). If the text looks like an name (no digits in it and has spaces), then it is a name. Else it is garbage.
0

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.

2 Comments

his "i" is moving one by one and is putting the name into "name[i]", so "Peter" and "Mary" would lead to an "PMary" string.
i change to the char and use strcmp. Actually, i want to abbr the word .The name of student_num was required in my project
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.