A data structure i created with a function

This is a discussion on A data structure i created with a function within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> struct { char Name[]; char address[]; float SS[]; int age[]; char gender[]; char statues[]; } void sentence( ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123

    A data structure i created with a function

    Code:
    #include <stdio.h>
    
    struct
    {
    char Name[];
    char address[];
    float SS[];
    int age[];
    char gender[];
    char statues[];
    }
    
    void sentence( char struct array[], char buffer[], int X);
    
    main(void)
    {
    struct info employees[10];
    int i=0,num;
    printf("How many employees you wish to log?\n");
    scanf("%d",num);
    
    if(num > 10){
    printf("Error only up to 10\n");
    printf("How many employees you wish to log?\n");
    scanf("%d",num); }
    
    printf("what are the employees names?\n");
    
    sentence(employees.Name[],num);
    
    printf("What are the addresses?\n");
    
    sentence(employees.address[],num);
    
    printf("What is there sex?\n");
    
    sentence(employees.gender[],num);
    
    printf("is the employees marry or single?\n");
    
    sentence(employees.statues[],num);
    
    for(i=0, i != num, ++ i) 
    {
    printf("What is employees ages?\n");
    scanf("%d",&employees.age[60]);
    printf("What is employees social security\n");
    scanf("%f",&employees.ss[10]);
    }
    
    
    }
    
    
     void sentence(Char struct array[], char  buffer[],int x)
    {
      int i, z;
    char letter;
    
    for(i = 0; i != x; ++ i) {
    do 
    {
     letter = getchar();
     array[i].buffer[z] = letter;
    ++ z;
    } while(letter != '\n');
    
    array[i].buffer[z-1] = '\0';
    
    printf("%s",array[i].buffer);
    
    }

    i have a problem bring a structure into a function...

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You have a problem with basic syntax.
    Code:
    struct
    {
    char Name[];
    char address[];
    float SS[];
    int age[];
    char gender[];
    char statues[];
    }
    These arrays need sizes, and the struct ought to have a name and a closing semicolon.
    Code:
    void sentence( char struct array[], char buffer[], int X);
    What's that?
    Code:
       sentence(employees.Name[],num);
    If you intend to specify an element, then specify an element. If you mean the whole array, then omit the []. If you mean an element of employees, move the brackets.
    Code:
    main(void)
    Wake up and smell 1990.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    Quote Originally Posted by Dave_Sinkula
    You have a problem with basic syntax.
    Code:
    struct
    {
    char Name[];
    char address[];
    float SS[];
    int age[];
    char gender[];
    char statues[];
    }
    These arrays need sizes, and the struct ought to have a name and a closing semicolon.
    Code:
    void sentence( char struct array[], char buffer[], int X);
    What's that?
    Code:
       sentence(employees.Name[],num);
    If you intend to specify an element, then specify an element. If you mean the whole array, then omit the []. If you mean an element of employees, move the brackets.
    Code:
    main(void)
    Wake up and smell 1990.
    No is not the program is that i wanted to know a good example of putting a structure element into an function..my program is just a draft in other words(soo i left alot of things out)

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    Much of Dave's advice is still relevant though because before you can pass a structure to a function, the structure needs to be declared properly and usually with a name. Then you can pass it to a function just like you would any other data type.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    First of all u need a proper indentation.

    You need to still understand the language syntax. Go through the code again and again find out the changes and research why those changes are made.

    Note: This is only to read a single employee details and i haven't done error checking i will leave it to u to do that

    Code:
    #include <stdio.h>
    
    struct info
    {
    char Name[20];
    char address[30];
    float SS;
    int age;
    char gender[20];
    char statues[20];
    };
    
    void sentence(char array[]);
    void clear_buffer(void);
    
    int main(void)
    {
        struct info employees[10];
        int i=0,num;
    
        printf("How many employees you wish to log?\n");
        scanf("%d",&num); /* u need to give the address of num variable hence &num*/
        clear_buffer();
        
        if(num > 10)
        {
            printf("Error only up to 10\n");
            printf("How many employees you wish to log?\n");
            scanf("%d",&num); /* u need to give the address of num variable hence &num*/
        }
        
        printf("what are the employees names?\n");
        sentence(employees[num].Name); /* Look at your function prototype it dosn't take 3 parameter */
    
        printf("What are the addresses?\n");
        sentence(employees[num].address);
        
        printf("What is there sex?\n");
        sentence(employees[num].gender);
    
        printf("is the employees marry or single?\n");
        sentence(employees[num].statues);
        
        printf("What is employees ages?\n");
        scanf("%d",&employees[num].age);
            
        printf("What is employees social security\n");
        scanf("%f",&employees[num].SS);
    
        /* print your struct elements for testing your struct */
        return 0;
    }
    
    /* you function takes three parameter which is not reuqured */
    void sentence(char array[])
    {
      int i, z=0;
      char letter;
      
        do 
        {    
            letter = getchar();
            array[z] = letter;
            ++ z;
        } while(letter != '\n');
        
        array[z-1] = '\0';
    }
    
    void clear_buffer(void)
    {
        int ch;
        while((ch=getchar())!='\n');
    }
    ssharish2005
    Last edited by ssharish2005; 11-11-2006 at 10:41 AM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    well thank you for the help. i'll see to it i keep alternating my program....

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    i still have problems with it...i want to input multiple employee's information but i mess up with calling structures into a function....

    Code:
    #include <stdio.h>
    
    struct info
    {
    char Name;
    char address;
    float SS;
    int age;
    char gender;
    char statues;
    };
    
    void sentence(char array[]);
    void clear_buffer(void);
    
    int main(void)
    {
        struct info employees[10];
        int i=0,num;
    
        printf("How many employees you wish to log?\n");
        scanf("%d",&num); /* u need to give the address of num variable hence &num*/
        clear_buffer();
        
        if(num > 10)
        {
            while( num < 10 ) {
            printf("Error only up to 10\n");
            printf("How many employees you wish to log?\n");
            scanf("%d",&num); /* u need to give the address of num variable hence &num*/
     		     }
        }
        for( i = 0; i > num; ++ i)
        printf("what are the employees names?\n");
        sentence(employees[i].Name); /* Look at your function prototype it dosn't take 3 parameter */
    
        printf("What are the addresses?\n");
        sentence(employees[i].address);
        
        printf("What is there sex?\n");
        sentence(employees[i].gender);
    
        printf("is the employees marry or single?\n");
        sentence(employees[i].statues);
        
        printf("What is employees ages?\n");
        scanf("%d",&employees[i].age);
            
        printf("What is employees social security\n");
        scanf("%f",&employees[i].SS);
    
       
        return 0;
    }
    
    
    void sentence(char array[])
    {
      int  z=0;
      char letter,*sentence_pointer,  buffer[90];
      
        do 
        {    
            letter = getchar();
            buffer[z] = letter;
            ++ z;
        } while(letter != '\n');
        
        buffer[z-1] = '\0';
        sentence_pointer =&buffer;
        array[] <- sentence_pointer;
    }
    
    void clear_buffer(void)
    {
        int ch;
        while((ch=getchar())!='\n');
    }
    basically it want a char to be inputted in but i don't want to type cast seen it will mess up with my for...

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    /* This is a functon which is been used to read the data from the user and place that
    in the data element of the struct
    
    for example:
        sentence(employees[i].Name);  // You send the char array to the function to get the data
                                        and place that in the char array which u have sent in this 
                                        case its employee[i].Name
    
    When you send the string to the function its using a method called call by reference. which meains 
    what every u change the value in the fucntion it reflects the original value. Where u dont need to 
    return the value back to the main.
    */
    
    void sentence(char array[])
    {
      int  z=0;   // count variable
      char letter;   // Letter used to hold char which is read from the user
      
        do 
        {    
            letter = getchar();   // get the char and place that in the array which is nothing but 
                                    // placing it in the employees[i].Name. no dont need to return this 
                                    // this value as it been sent using call by refernce method.
            array[z] = letter;
            ++ z;
        } while(letter != '\n' && z < strlen(array));   // included boundary checking  
        
        buffer[z-1] = '\0';  // Sepcify end of the string
        
        /* note no values returned */
    }
    U got messed up with the sentence function. If u wanted to get multiple input its all do to in the main not in the function sentence.

    U'r for loop is wrong
    Code:
    for( i = 0; i > num; ++ i)
    
    to 
    
    for(i=0; i < num; i++)

    sshaish2005

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    i call it by reference but i get an error saying
    35 C:\Documents and Settings\Valued Customer\My Documents\project file\lab.c [Warning] passing arg 1 of `sentence' makes pointer from integer without a cast
    for each time i put employees.etc into the sentence...
    Code:
    #include <stdio.h>
    
    struct info
    {
    char Name;
    char address;
    float SS;
    int age;
    char gender;
    char statues;
    };
    
    void sentence(char array[]);
    void clear_buffer(void);
    
    int main(void)
    {
        struct info employees[10];
        int i=0,num;
    
        printf("How many employees you wish to log?\n");
        scanf("%d",&num); /* u need to give the address of num variable hence &num*/
        clear_buffer();
        
        if(num > 10)
        {
            while( num < 10 ) {
            printf("Error only up to 10\n");
            printf("How many employees you wish to log?\n");
            scanf("%d",&num); /* u need to give the address of num variable hence &num*/
     		     }
        }
        for( i = 0; i < num; ++ i)
        printf("what are the employees names?\n");
        sentence(employees[i].Name); /* Look at your function prototype it dosn't take 3 parameter */
    
        printf("What are the addresses?\n");
        sentence(employees[i].address);
        
        printf("What is there sex?\n");
        sentence(employees[i].gender);
    
        printf("is the employees marry or single?\n");
        sentence(employees[i].statues);
        
        printf("What is employees ages?\n");
        scanf("%d",&employees[i].age);
            
        printf("What is employees social security\n");
        scanf("%f",&employees[i].SS);
    
       
        return 0;
    }
    
    
    void sentence(char array[])
    {
      int  z=0;   // count variable
      char letter;   // Letter used to hold char which is read from the user
      
        do 
        {    
            letter = getchar();   // get the char and place that in the array which is nothing but 
                                    // placing it in the employees[i].Name. no dont need to return this 
                                    // this value as it been sent using call by refernce method.
            array[z] = letter;
            ++ z;
        } while(letter != '\n' && z < strlen(array));   // included boundary checking  
        
        array[z-1] = '\0';  // Sepcify end of the string
        
        /* note no values returned */
    }
    
    
    void clear_buffer(void)
    {
        int ch;
        while((ch=getchar())!='\n');
    }

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    char Name;
    Name is not array - it is one char...

    To make it array deeclare it as
    Code:
    char Name[30]
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by Darkinyuasha1
    i call it by reference but i get an error saying
    35 C:\Documents and Settings\Valued Customer\My Documents\project file\lab.c [Warning] passing arg 1 of `sentence' makes pointer from integer without a cast
    for each time i put employees.etc into the sentence...
    Code:
    #include <stdio.h>
    
    struct info
    {
    char Name;
    char address;
    float SS;
    int age;
    char gender;
    char statues;
    };
    
    void sentence(char array[]);
    void clear_buffer(void);
    
    int main(void)
    {
        struct info employees[10];
        int i=0,num;
    
        printf("How many employees you wish to log?\n");
        scanf("%d",&num); /* u need to give the address of num variable hence &num*/
        clear_buffer();
        
        if(num > 10)
        {
            while( num < 10 ) {
            printf("Error only up to 10\n");
            printf("How many employees you wish to log?\n");
            scanf("%d",&num); /* u need to give the address of num variable hence &num*/
     		     }
        }
        for( i = 0; i < num; ++ i)
        printf("what are the employees names?\n");
        sentence(employees[i].Name); /* Look at your function prototype it dosn't take 3 parameter */
    
        printf("What are the addresses?\n");
        sentence(employees[i].address);
        
        printf("What is there sex?\n");
        sentence(employees[i].gender);
    
        printf("is the employees marry or single?\n");
        sentence(employees[i].statues);
        
        printf("What is employees ages?\n");
        scanf("%d",&employees[i].age);
            
        printf("What is employees social security\n");
        scanf("%f",&employees[i].SS);
    
       
        return 0;
    }
    
    
    void sentence(char array[])
    {
      int  z=0;   // count variable
      char letter;   // Letter used to hold char which is read from the user
      
        do 
        {    
            letter = getchar();   // get the char and place that in the array which is nothing but 
                                    // placing it in the employees[i].Name. no dont need to return this 
                                    // this value as it been sent using call by refernce method.
            array[z] = letter;
            ++ z;
        } while(letter != '\n' && z < strlen(array));   // included boundary checking  
        
        array[z-1] = '\0';  // Sepcify end of the string
        
        /* note no values returned */
    }
    
    
    void clear_buffer(void)
    {
        int ch;
        while((ch=getchar())!='\n');
    }

    you dint actually look at my code properly. Go through it again the answer is in there

    ssharish2005

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    yea but even if i do that it would say the same thing

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    ooh i see i got an error -^^- forgot to put the braces for "for" heh

  14. #14
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    This is what i did as i alter it
    Code:
    #include <stdio.h>
    
    struct info
    {
    char Name[30];
    char address[30];
    float SS;
    int age;
    char gender[30];
    char statues[30];
    };
    
    void sentence(char array[]);
    void clear_buffer(void);
    
    int main(void)
    {
        struct info employees[10];
        int i=0,num;
    
        printf("How many employees you wish to log?\n");
        scanf("%d",&num); /* u need to give the address of num variable hence &num*/
        clear_buffer();
        
        if(num > 10)
        {
            while( num < 10 ) {
            printf("Error only up to 10\n");
            printf("How many employees you wish to log?\n");
            scanf("%d",&num); /* u need to give the address of num variable hence &num*/
     		     }
        }
       
        printf("what are the employees names?\n");
       sentence(employees[num].Name); /* Look at your function prototype it dosn't take 3 parameter */
    
        printf("What are the addresses?\n");
        sentence(employees[num].address);
        
        printf("What is there sex?\n");
        sentence(employees[num].gender);
    
        printf("is the employees marry or single?\n");
        sentence(employees[num].statues);
        
        printf("What is employees ages?\n");
        scanf("%d",&employees[num].age);
            
        printf("What is employees social security\n");
        scanf("%f",&employees[num].SS);
                  
    
       
        return 0;
    }
    
    
    void sentence(char array[])
    {
      int  z=0;   // count variable
      char letter;   // Letter used to hold char which is read from the user
      
        do 
        {    
            letter = getchar();   // get the char and place that in the array which is nothing but 
                                    // placing it in the employees[i].Name. no dont need to return this 
                                    // this value as it been sent using call by refernce method.
            array[z] = letter;
            ++ z;
        } while(letter != '\n' && z < strlen(array));   // included boundary checking  
        
        array[z-1] = '\0';  // Sepcify end of the string
        
        /* note no values returned */
    }
    
    
    void clear_buffer(void)
    {
        int ch;
        while((ch=getchar())!='\n');
    }
    Last edited by Darkinyuasha1; 11-13-2006 at 08:56 AM.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Darkinyuasha1
    ok this is what i did soo far i guess
    And?

    You can't expect everyone here to grab a compiler, create a project or file, copy your code into it, and attempt compile, just to see what errors, warnings, or incorrect input or output you're getting.

    Learn to ask questions the smart way. It's good advice, even if you don't like hearing it.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. writting data from structure in different function
    By Shadow in forum C++ Programming
    Replies: 2
    Last Post: 08-09-2002, 08:52 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21