user3041923
user3041923

Reputation: 19

How to print specific data in c?

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

Answers (5)

chux
chux

Reputation: 153338

  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);

Upvotes: 1

H_squared
H_squared

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

G one
G one

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

Peter Miehle
Peter Miehle

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

Joze
Joze

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

Related Questions