-1

I could not find specific student_Id info from a rage of given info.suppose, I am taking input from 1 to 100 all student info. now I want to find only 50th number student_ID all info.i could not do it. how it possible.here show my code. what's wrong with my code and how fixed it.thanks

    # include <string.h>
    # include <stdio.h>
    # include <stdlib.h>
    # include <conio.h>
    struct student
    {
        char student_id[100];
        char name[10];
        int m[50],credit[100],sub[100];
        int total,sumCredit;
        float GP[100];
        char result[5];
        char  grade[100][10];
         double sumCreditxGP;
    }*p,*s;
    float gradesToGP(float marks);
    char *markToG(float gp);
    void lines(void);
    void main()
    {
        int i,j,l,n,sub,k;
        // clrscr();
        printf("Enter the no. of students : ");
        scanf("%d",&n);

        p=(struct student*) malloc(n*sizeof(struct student));
        //printf("%d",p);
        //exit(0);
        s=p;
        for(i=0; i<n; i++)
        {
            printf("Enter a student id : ");
            scanf("%s",&p->student_id);
            printf("Enter a name : ");
            scanf("%s",&p->name);
            printf("Enter the no. of subject : ");
            scanf("%d",&p->sub[i]);
            p-> total=0;
            p-> sumCredit=0;
            p-> sumCreditxGP=0;
            l=0;
            for(j=0; j<p->sub[i]; j++)
            {
    one:
                printf("Enter Marks of %d Subject\t%d : ",j+1,p->sub[i]);
                scanf("%d",&p->m[j]);
                printf("Enter Credit Hour: ");
                scanf("%d",&p->credit[j]);
                p->GP[j] = gradesToGP((float)p->m[j]);
                strcpy(p->grade[j],markToG(p->m[j]));
                if((p->m[j])>100)
                {
                    printf("---------------Wrong Value Entered-----------\n");
                    goto one;
                }
                p->total+=p->m[j];
                p->sumCredit+=p->credit[j];
                 p->sumCreditxGP+=p->credit[j]*p->GP[j];
                if(p->m[j]<40)
                    l=1;
            }
            if(l==0)
                strcpy(p->result,"PASS");
            else
                strcpy(p->result,"FAIL");
            p++;
        }

 char search_id[10];
       printf("Enter id to find specific student ");
       scanf("%s",search_id);

       //PROBLEM START HERE
        for(i=0; i<n; i++)
        {
            if(p->student_id==search_id){
            printf("found");
            printf("%s",s->student_id);
            }else{
                 printf("Not found");
            }
            s++;
        }
        getch();
    }
    float gradesToGP(float marks)
    {
        if (marks>=80 && marks<=100)
        {
            return(float)4.00;
        }
        else if (marks>=75 && marks<=79)
        {
            return(float)3.67;
        }
        else if (marks>=70 && marks<=74)
        {
            return(float)3.33;
        }
        else if (marks>=65 && marks<=69)
        {
            return(float)3.00;
        }
        else if (marks>=60 && marks<=64)
        {
            return(float)2.67;
        }
        else if (marks>=55 && marks<=59)
        {
            return(float)2.33;
        }
        else
        {
            return(float)5.00;
        }
    }
    char *markToG(float marks)
    {
        if (marks>=80 && marks<=100)
        {
            return "A";
        }
        else if (marks>=75 && marks<=79)
        {
            return "A-";
        }
        else if (marks>=70 && marks<=74)
        {
            return "B+";
        }
        else if (marks>=65 && marks<=69)
        {
            return "B";
        }
        else if (marks>=60 && marks<=64)
        {
            return "B-";
        }
        else if (marks>=55 && marks<=59)
        {
            return "C+";
        }
        else
        {
            return "null";
        }
    }
    void lines(void)
    {        printf("**********************************************************************");
    }

Please tell me how can I fixed it.thanks in advanced.

Roberto Pinheiro
  • 1,260
  • 13
  • 30
virtual
  • 3
  • 4
  • 1
    This code is far from a [mcve]... In addition, the answer if is [C tag FAQ](https://stackoverflow.com/tags/c/info): [How do I properly compare strings?](https://stackoverflow.com/q/8004237/3545273) – Serge Ballesta Mar 27 '18 at 11:47

2 Answers2

1
if(p->student_id==search_id){
            printf("found");

Now, that's not how you compare strings in C. Use the strcmp() function for string comparison. You may read about strcmp() here.

babon
  • 3,615
  • 2
  • 20
  • 20
0

The issue is your equality check: if(p->student_id==search_id)

Because both student_id and search_id are char arrays, the types will decay to pointers (char *) and this will never work as you expect. Instead, you need to use strcmp (or better, strncmp).

if(strncmp(p->student_id, search_id, 10) == 0) { /* equality */ }
Stephen Newell
  • 7,330
  • 1
  • 24
  • 28