Thread: Hello! Need a little help with c structures assignment

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    3

    Hello! Need a little help with c structures assignment

    Code:
    [/
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    
    struct Student
    {
        char fname[20], lname[20], id[10];
        float golch ;
    };
    typedef struct Student s1 ;
    //by the way golch means GPA//
    
    
    
    
    void read_students (s1 a[], int n)
    {
        int i;
        for(i=0; i<n; i++)
        {
            scanf("%s %s %s %f",&a[i].fname, &a[i].lname, &a[i].id, &a[i].golch);
        }
    }
    
    
    void print_students (s1 a[], int n)
    {
        int i;
        for(i=0; i<n; i++)
        {
            printf("%s %s %s %.1f\n",a[i].fname, a[i].lname, a[i].id, a[i].golch);
        }
    }
    
    
    int search_by_fname (s1 a[], int n, char fname[])
    {
        int i;
        for(i=0; i<n; i++)
        {
            if(strcmp(a[i].fname, fname)==0)
            {
                return i;
            }
            else
                return -1;
        }
    }
    
    
    int search_by_lname (s1 a[], int n, char lname[])
    {
        int i;
        for(i=0; i<n; i++)
        {
            if(strcmp(a[i].lname, lname)==0)
            {
                return i;
            }
            else
                return -1;
        }
    }
    int search_by_id (s1 a[], int n, char id[])
    {
        int i;
        for(i=0; i<n; i++)
        {
            if(strcmp(a[i].id, id)==0)
            {
                return i;
            }
            else
                return -1;
        }
    }
    
    
    int search_by_golch (s1 a[], int n, float golch )
    {
        int i;
        for(i=0; i<n; i++)
        {
            if(golch == a[i].golch)
                return  i;
            else
                return  -1;
        }
    }
    
    
    void sort_by_golch (s1 a[], int n)
    {
        int i,j;
        char tmp[25];
        float f;
        for(i=0; i<n; i++)
        {
            for(j=i+1; j<n; j++)
            {
                if(a[i].golch<a[j].golch)
                {
                    strcpy(tmp, a[i].fname);
                    strcpy(a[i].fname, a[j].fname);
                    strcpy(a[j].fname, tmp);
                    strcpy(tmp, a[i].lname);
                    strcpy(a[i].lname, a[j].lname);
                    strcpy(a[j].lname, tmp);
                    strcpy(tmp, a[i].id);
                    strcpy(a[i].id, a[j].id);
                    strcpy(a[j].id, tmp);
                    f=a[i].golch;
                    a[i].golch=a[j].golch;
                    a[j].golch=f;
    
    
                }
            }
        }
    }
    
    
    
    
    int main()
    {
        s1 a[100];
        int n, cmd, idx;
        printf("Oyutnii too : ");
        scanf( "%d", &n) ;
        read_students(a, n) ;
        print_students(a, n) ;
        char fname[20], lname[20], id[20];
        float golch ;
        while (1)
        {
            printf( "1: Search by name, 2: search by last name, 3: search by ID number, 4: Search by GPA, 5: Sort by GPA, 6: Print, 7: Exit \n" ) ;
            scanf( "%d", &cmd) ;
            if(cmd == 1)
            {
                printf( "First name : " ) ;
                scanf( "%s", fname) ;
                idx = search_by_fname(a, n, fname) ;
                if(idx == -1)
                    printf( "No match\n" ) ;
                else
                    print(a[idx] ) ;
            }
            else if(cmd == 2)
            {
                printf ( "Last Name : " ) ;
                scanf ( "%s", lname) ;
                idx = search_by_lname (a, n, lname) ;
                if( idx == -1)
                    printf ( "No match\n" ) ;
                else
                    print (a[idx]) ;
            }
            else if(cmd == 3)
            {
                printf ( "ID number : " ) ;
                scanf ( "%s", id ) ;
                idx = search_by_id (a, n, id ) ;
                if( idx == -1)
                    printf( "No match\n" ) ;
                else
                    print(a[idx] ) ;
            }
            else if(cmd == 4)
            {
                printf( "GPA: " ) ;
                scanf( "%f ", &golch ) ;
                idx = search_by_golch(a, n, golch ) ;
                if(idx == -1)
                    printf ("No match\n") ;
                else
                    print(a[idx]) ;
            }
            else if(cmd == 5)
                sort_by_golch(a, n) ;
            else if(cmd == 6)
                print_students(a, n) ;
            else
                break;
        }
        return 0;
    }
    ]

  2. #2
    Registered User
    Join Date
    Dec 2018
    Posts
    3
    Does anyone know why does this issue persist? Any help would be appreciated!

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Guest
    Guest
    Would compiler are you using? It would be best if you simply looked at compiler warnings to iron out all errors.
    Code:
        for(i=0; i<n; i++)
        {
            if(strcmp(a[i].fname, fname)==0)
            {
                return i;
            }
            else
                return -1;
        }
    This is probably not the logic you want. This loop with always return on its first iteration – think about it.

  5. #5
    Registered User
    Join Date
    Dec 2018
    Posts
    3
    error: 'print' was not declared in this scope on line 149 159 169 179;

  6. #6
    Guest
    Guest
    Code:
    prog.c: In function 'read_students':
    prog.c:22:17: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[20]' [-Wformat=]
       22 |         scanf("%s %s %s %f",&a[i].fname, &a[i].lname, &a[i].id, &a[i].golch);
          |                ~^           ~~~~~~~~~~~
          |                 |           |
          |                 char *      char (*)[20]
    prog.c:22:20: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[20]' [-Wformat=]
       22 |         scanf("%s %s %s %f",&a[i].fname, &a[i].lname, &a[i].id, &a[i].golch);
          |                   ~^                     ~~~~~~~~~~~
          |                    |                     |
          |                    char *                char (*)[20]
    prog.c:22:23: warning: format '%s' expects argument of type 'char *', but argument 4 has type 'char (*)[10]' [-Wformat=]
       22 |         scanf("%s %s %s %f",&a[i].fname, &a[i].lname, &a[i].id, &a[i].golch);
          |                      ~^                               ~~~~~~~~
          |                       |                               |
          |                       char *                          char (*)[10]
    prog.c: In function 'main':
    prog.c:148:17: warning: implicit declaration of function 'print'; did you mean 'printf'? [-Wimplicit-function-declaration]
      148 |                 print(a[idx] ) ;
          |                 ^~~~~
          |                 printf
    prog.c: In function 'search_by_fname':
    prog.c:49:1: warning: control reaches end of non-void function [-Wreturn-type]
       49 | }
          | ^
    prog.c: In function 'search_by_lname':
    prog.c:64:1: warning: control reaches end of non-void function [-Wreturn-type]
       64 | }
          | ^
    prog.c: In function 'search_by_id':
    prog.c:77:1: warning: control reaches end of non-void function [-Wreturn-type]
       77 | }
          | ^
    prog.c: In function 'search_by_golch':
    prog.c:90:1: warning: control reaches end of non-void function [-Wreturn-type]
       90 | }
          | ^
    /tmp/cc8YluFe.o: In function `main':
    prog.c:(.text+0x78d): undefined reference to `print'
    prog.c:(.text+0x842): undefined reference to `print'
    prog.c:(.text+0x8f7): undefined reference to `print'
    prog.c:(.text+0x9ad): undefined reference to `print'

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Where did you define "print" ? Or did you mean to use printf?

    And this and the other similar functions are obviously wrong. Do you see why? Follow the logic and see what happens if the first name in "a" doesn't match fname?
    Code:
    int search_by_fname (s1 a[], int n, char fname[])
    {
        for (int i = 0; i < n; i++)
        {
            if (strcmp(a[i].fname, fname) == 0)
                return i;
            else
                return -1;
        }
    }
    BTW, s1 is a meaningless name. Why wouldn't you just call it Student? And "a" is also totally meaningless. I would call it "students".
    And it is very old-fashioned to put "int i" before the loop instead of inside the head of the loop.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Problem (Structures)
    By Winter in forum C Programming
    Replies: 4
    Last Post: 11-05-2015, 11:37 PM
  2. Replies: 3
    Last Post: 04-22-2015, 02:44 PM
  3. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  4. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread