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++;
}   
  • 4
    Technically, `fflush(stdin);` is undefined. And with your program it's not needed. – Some programmer dude Dec 02 '13 at 15:40
  • 1
    As for your problem, have you tried a loop around the input for the id and the check? – Some programmer dude Dec 02 '13 at 15:42
  • why the int-array for student_id? – Peter Miehle Dec 02 '13 at 15:44
  • 2
    @JoachimPileborg: On [Windows](http://msdn.microsoft.com/en-us/library/9yky46tz.aspx), `fflush(stdin)` is technically *defined* by Microsoft. In fact, I learned last week, the same is true on [Linux](http://linux.die.net/man/3/fflush) (_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. – Jonathan Leffler Dec 02 '13 at 15:48
  • why `student_id[30]`? How many IDs can one student have? – H_squared Dec 02 '13 at 16:02
  • Please buy a book on C. :-D – Fiddling Bits Dec 02 '13 at 16:59
  • Joachim Pileborg i tried it. Peter Miehle the user type number so i put int-array.Actually,the original type of id is char ,but i have no idea why it is char.So i chang it. Jonathan Leffler ,thanks i will modify the problem. hhachem,it is only 8 digits. – user3041923 Dec 02 '13 at 17:16
  • @user3041923 When you declare your struct like that, you get 30 IDs per student. You meant to declare a struct array. Check this: http://stackoverflow.com/questions/10468128/how-do-you-make-an-array-of-structs-in-c – H_squared Dec 03 '13 at 07:46

5 Answers5

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];
Peter Miehle
  • 5,984
  • 2
  • 38
  • 55
  • 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. – user3041923 Dec 02 '13 at 17:18
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++; }   
G one
  • 2,679
  • 2
  • 14
  • 18
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);

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 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 Dec 02 '13 at 18:32
  • @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. – chux - Reinstate Monica Dec 02 '13 at 18:52
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.

Joze
  • 1,285
  • 4
  • 19
  • 33
  • 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. – Peter Miehle Dec 02 '13 at 15:48
  • 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 – user3041923 Dec 02 '13 at 17:47
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

H_squared
  • 1,251
  • 2
  • 15
  • 32