Thread: Complex numbers and C structures

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by overlord21 View Post
    hello guys i got this exercice in which i need help. This program should fill two arrays made of information about students and employee.

    i have to sort alphabetically each of them by name and last name, still i have to check that no name should be repeated. In this program i tried to store the information in a table string"std" and in the same time check for any repeated name but i have problem with the sorting and when i run this it shuts down as the size ins't recognized even if i specified it.
    Okay we clearly have a lot to do so lets break down the assignment and what we have so far. That's step one when you start to think about programming, by the way. My professor used to tell me that every hour spent planning saves two hours in coding time. Jumping right in and just attempting to debug the whole solution at once is a mess.

    You must:
    • collect data about students and employees in an array
      • delete repeated names
    • sort the data alphabetically by surname


    Let's see how you tackled the first bullet:
    Code:
    typedef struct {
             char *firstname;
             char *lastname;
             float grade;
             }student;
             
    typedef struct {
            char *lastname;
            char *firstname;
            float salary;
            }employee;
    
    void fill_e(employee *e)
    {
         printf("please enter first name");
         scanf("%s",&e->firstname);
         printf("please enter last name");
         scanf("%s",&e->lastname);
         printf("please enter grade");
         scanf("%f",&e->salary);
         
         } 
    
    void fill_s(student *s)
    {
         printf("please enter first name");
         scanf("%s",&s->firstname);
         printf("please enter last name");
         scanf("%s",&s->lastname);
         printf("please enter grade");
         scanf("%f",&s->grade);
         
         }
    While it is good that strudent and employee have name fields that don't impose a hard limit on the length of the name, you forgot completely to allocate memory. Accessing those fields at all will result in undefined behavior and most likely cause a program crash. To avoid that, perhaps you should rely on arrays for now, during development, and introduce malloc'd strings later, when the program is almost done.

    And scanf isn't one of the most trustworthy ways to read strings because of the possibility you'll write past an array boundary, invoking undefined behavior again. I really would choose a different approach for input, like fgets() & sscanf(). We can get the information we need using fgets() to fill name fields. To get the grade and salaries, we could use sscanf(). sscanf() just formats input from a string like scanf would.

    Also, there isn't a problem with your search, but arrays cannot be resized should you need to remove a record like in bullet one. So you need to search for a person of the same name during the input stage, before the record is added to the array. Then you can ask the user for a different name.

    So maybe you can work with this at first:
    Code:
    #define NAMESIZE 512
    #define SIZE 10
    
    #include <stdio.h>
    #include <string.h>
    
    struct EmployeeTag {
       float salary;
       char firstname[NAMESIZE];
       char lastname[NAMESIZE];
    };
    
    struct StudentTag {
       float grade;
       char firstname[NAMESIZE];
       char lastname[NAMESIZE];
    };
    
    typedef struct EmployeeTag Employee;
    typedef struct StudentTag Student;
    
    Employee * newEmployee (Employee * e, Employee records[SIZE]);
    int search (Employee * worker, Employee records[SIZE]);
    
    
    Employee * newEmployee (Employee * worker, Employee records[SIZE]) 
    {
        char input[NAMESIZE * 2];
        printf("Enter employee's last name:\n");
        if (fgets(input, NAMESIZE, stdin) != NULL) {
            char * end = strchr(input, '\n');
            if (nl != NULL)
                *nl = '\0';
            strcpy(worker->lastname, input);
       }
       printf("Enter the employee's first name:\n");
       if (fgets(input->firstname, NAMESIZE, stdin) != NULL) {
           char * end = strchr(input, '\n');
           if (nl != NULL)
               *nl = '\0';
           strcpy(worker->firstname, input);
       }
       printf("Enter the employee's salary:\n");
       if (fgets(input, sizeof input, stdin) != NULL) {
           sscanf(input, "%f", &worker->salary);
       }
       if (search(worker, records) == 1) 
           return NULL;
       else
           return worker;
    }
    Now you can use newEmployee() to fill in any part of the array, and depending on what it returns if it was successful or not. A similar process should be made for students.

    Next comes solving bullet two -- sorting. You should select an algorithm and try to implement it.

    Let's see what you come up with now. HTH.
    Last edited by whiteflags; 09-11-2008 at 02:29 PM.

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    And don't use conio.h. It's an evil non-standard header.
    Nonstandard does not an evil library make. Most compilers that target Win32 or MS-DOS will include a conio header and it really depends on for whom this program is intended and what parts of the library is being used, like any library.

  3. #18
    Registered User
    Join Date
    May 2008
    Posts
    72
    the programs bugs out when removing the "&"

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Where is the memory for lastname and firstname allocated?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    Registered User
    Join Date
    May 2008
    Posts
    72
    hey guys, can anyone tell me just( functions or instruction) on how to make the previous exercice using a sequential text file?

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Read from a file instead of reading from stdin?

Popular pages Recent additions subscribe to a feed