Thread: struct problem (help)

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    19

    struct problem (help)

    Code:
     struct groups {
           
           char name_group[NAMES];      
           char ID_group[NUMB];
           char add_ID_employee[NUMB];
           
           } group[MAX_GROUPS];
    
    struct employees {
           
           char name_employee[NAMES];
           char ID_employee[NUMB];
           } employee[MAX_EMPLOYEES];
    I have this 2 structures.
    I can add employees and add groups.

    I want to link various employees to certain groups through they ID_employee.

    I have this function to do that:

    Code:
     AddEmployeesGroup(){
                  
          int i=0, j=0, numb_employees;               
          char option_group[50];
          
          printf("\nGROUPS IN THE SYSTEM:\n");
          for(i=0;i<total_groups;i++){
          printf("\n%s", group[i].name_group);                       
          }
          
          printf("\nEMPLOYEES IN THE SYSTEM:\n");
          for(i=0;i<total_employees;i++){
          printf("\nName: %s ID: %s", employee[i].ID_employee, pessoal[i].numero_id_pessoal);                       
          } 
          
          printf("\nPick a group:\n");
          scanf(" ");
          gets(option_group);
          
    /*search to see the position (i) of the group where I want to link
    the ID of the employees*/
    
          for(i=0;i<total_groups;i++){
                           
            if(strcmp(group[i].name_group, option_group) == 0)
            break;
            }  
            //now I know the position
            
          printf("\nHow many employees do you want to add?\n");
          scanf(" %i", &numb_employees);
           
          for(j=0;j<numb_employees;j++){
          printf("\nWhat's the ID of the employees that you want to add to shis group?:");
          scanf(" ");
    
          gets(group[i].add_ID_employee[PositionEmployees+j]);
    //passing arg 1 of gets' makes pointer from integer without a cast
          }
          PositionEmployees=j;
    }
    The variable PositionEmployees is declared in the beggining of the program and is to detect the position that are occupied.

    In the last gets is my problem, in theory this should work, i have the position (i) of the group and now I will only have to input the ID's of the employees to "add_ID_employee"

    Can you guys give me any tips?
    Last edited by Gotze; 01-19-2012 at 11:16 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    For starters, you'd better use fgets instead of gets. See why here.

    About your problem, you pass a character and not a string at the last gets.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    19
    group[i] is a array of structures and add_ID_employee[NUMB] is, or should be, an array in the structure group.
    Can you tell me how to fix please?
    I've been working on this all day(and 7 PM in my country)

    I know that the problem is in this part [PositionEmployees+j]...

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You have misunderstood how arrays work. Yes, add_ID_employee is an array, but "add_ID_employee[i]" is a character a.k.a "dereferenced pointer". You could declare it as "char add_ID_employee[NUMB][64]" for example, to serve your needs.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    19
    I forgot to put here one important thing...

    Code:
    #include <stdio.h>
    #include <string.h>
    #define NAMES 500
    #define MAX_EMPLOYEES 1000
    #define MAX_GROUPS 300
    #define NUMB 1000

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct struct struct problem....please....help!!!
    By nullifyed in forum C Programming
    Replies: 5
    Last Post: 06-19-2010, 08:19 AM
  2. problem with struct
    By nevrax in forum C Programming
    Replies: 13
    Last Post: 05-02-2007, 11:38 AM
  3. Struct Problem
    By ManiacBR in forum C++ Programming
    Replies: 10
    Last Post: 11-08-2006, 11:34 AM
  4. Struct problem
    By chr15 in forum C Programming
    Replies: 4
    Last Post: 10-18-2006, 02:37 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM