Thread: Help checking a character in a structure

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    7

    Help checking a character in a structure

    I am currently writing a function that checks whether a character string contains an "F" or "M". I have tried strcmp() and a if statement. Here is the code I have tried, any help would be much appreciated.
    Code:
    void printFemale(Employee s[], int num) {
        int i;
        char *F;
        char female[1];
        female[0] = 'F';
        *F = female[0];
         printf( "The Female Employees Are\n");
         for( i = 0; i < num; ++i )
        {
        Trying this statement --->   if (s[i].sex[i] == 'F')
    
        Also have tried ----->    if (strcmp(s[i].sex, *F) == 0){
                printf("");
                printf("%.2f\n", s[i].pay);
            }
                        
        }
    }
    The code for the struct is as follows:
    Code:
    typedef struct Employee
    {
        char first[8];
        char initial[2];
        char last[10];
        Address ad;
        int age;
        char sex[2];
        int years;
        double pay;
    }Employee;
    I have a number of other structures that work, but when checking the character in sex[2] I just seem to be missing something.

    Thank you again.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I commented your code to show the different problems with what you tried.
    Code:
    void printFemale(Employee s[], int num) {
        int i;
        char *F;  // this is a char pointer, but it doesn't point anywhere meaningful, it points to garbage
        char female[1];  // this doesn't need to be an array, a single char would work
        female[0] = 'F';  // you set the one char in the array to 'F' -- this is okay
        *F = female[0];  // you assign the char pointed to by F (in garbage memory) to 'F', from the array -- this will likely seg fault or cause other nasty problems
         printf( "The Female Employees Are\n");
         for( i = 0; i < num; ++i )
        {
        Trying this statement --->   if (s[i].sex[i] == 'F')  // if i is not 0 or 1, you go out of bounds of the sex array.  perhaps s[i].sex[0] would work better
     
        Also have tried ----->    if (strcmp(s[i].sex, *F) == 0){  // F is a char pointer.  if F actually pointed somewhere useful, *F would be the single char it pointed to, which is not what strcmp wants, it wants a char pointer
    
                printf("");
                printf("%.2f\n", s[i].pay);
            }
                         
        }
    }
    In your case, I would do
    Code:
    if (s[i].sex[0] == 'F')
    // or
    if (strcmp(s[i].sex, "F") == 0)
    Actually, if you really only intend to store a single character for the sex field in your struct, don't use an array/string, it just confuses you. Just make it char sex;

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    7
    I tried that before, the if statement which is

    Code:
    void printFemale(Employee s[], int num) {
        int i;
        
        
         printf( "The Female Employees Are\n");
         for( i = 0; i < num; ++i )
        {
            if (s[i].sex[0] == 'F'){
                printf("HELLO\n");
            }
    
        }
    }
    I think that there might be something wrong with the way the structure is set up. I have been playing around with this for hours.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    7
    Any ideas?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by WranglerYJ View Post
    Any ideas?
    My idea is that you are not calling the printFemale function correctly.

    Tim S.

    Code:
    #include <stdio.h>
    
    typedef struct Employee
    {
        char first[8];
        char initial[2];
        char last[10];
    /*    Address ad; */
        int age;
        char sex[2];
        int years;
        double pay;
    }Employee;
    
    void printFemale(Employee s[], int num);
    
    int main()
    {
        Employee empList[1] = {{"Test", "T","Test", 21, "F", 1, 50.00}};
    
        printFemale(empList,1);
        return 0;
    }
    
    void printFemale(Employee s[], int num) {
        int i;
    
    
         printf( "The Female Employees Are\n");
         for( i = 0; i < num; ++i )
        {
            if (s[i].sex[0] == 'F'){
                printf("HELLO\n");
            }
    
        }
    }
    Last edited by stahta01; 05-11-2012 at 07:49 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    7
    Something wrong with the parameters of the call? I thought it was supposed to be the structure and the then the integer in the call, but when I try Employee[] it says type not allowed and I have played with other ways of using something other than employ. Am i on the right track, or is there something else to consider?

    Thank you

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Read my prior post again; I updated it with good test code.

    After that, restate your question because I could not understand it.

    Edit2: "Employee" is a type not a variable name; you can NOT pass types to functions.
    Edit: You might need to review arrays http://www.cprogramming.com/tutorial/c/lesson8.html

    Tim S.
    Last edited by stahta01; 05-11-2012 at 08:03 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    7
    So you created an array that contains an "F", and then I can use that to check it against s[i].sex[0]? would i use strcmp. Brand new to programming so I'm sorry if I don't make myself clear. Really trying to understand this.

    Thanks again.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I created an array because your function needed one.
    The array contained elements of type Employee. The element had sex initialize to "F".
    NOTE: As suggested before (by anduril462), you should make sex a char type NOT char array.

    http://www.cprogramming.com/tutorial/c/lesson7.html

    Edit: I suggest you do all of the C tutorial to lesson 8.

    Tim S.
    Last edited by stahta01; 05-11-2012 at 08:09 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    7
    So inside of the function i created (well you did)
    Code:
    void printFemale(Employee s[], int num) {
        int i;
        Employee empList[1] = {{"F"}};
     
       
        
         printf( "The Female Employees Are\n");
         printf("%c", empList[0]);
         for( i = 0; i < num; ++i )
        {
            if (s[i].sex[0] == 'F'){
                printf("HELLO\n");
            }
    
        }
    }
    I read through the tutorial, and from your NOTE: in the struct declaration you two are saying to change char sex[2] to just char sex;. If I do that a lot of other things wont work. Is there a was I can :
    Code:
    if (strcmp(s[i].sex[0], emplist[0]) == 0)
         print();
    Thanks again for your help, I really appreciate it

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    7
    Help Please

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Don't bump your thread (read this: forum guidelines, rule #3). We'll get to it when we can -- we volunteer in our spare time after all, and we do have lives of our own .

    I gave you the strcmp solution in my original post. Also, look at your initialization of empList:
    Code:
    Employee empList[1] = {{"F"}};
    That is incorrect, and not what Tim posted.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Below is code that does not initial the array; instead it assigns the value after array creation.

    Note: I am not likely to help you anymore; you are too much near the beginning of learning C; I learned C decades ago. I really do not remember enough on how to teach the first 6 weeks of learning programming. Just giving you code like I did in this post is not the best teaching method.

    Tim S.

    Code:
    int main()
    {
        Employee empList[1];
    
        strcpy(empList[0].first, "first" );
        empList[0].initial[0] = 'I';
        strcpy(empList[0].last, "last" );
        empList[0].age = 21;
        empList[0].sex[0] = 'F';
        empList[0].pay = 50.00;
    
        printFemale(empList,1);
        return 0;
    }
    YOU NEED to follow and read the information on the links posted!

    strcpy - C++ Reference
    http://www.cprogramming.com/tutorial/c/lesson9.html
    Last edited by stahta01; 05-12-2012 at 09:10 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking the input is character or not
    By sal in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2012, 06:13 AM
  2. Checking if a character is a '\'
    By serg_yegi in forum C Programming
    Replies: 18
    Last Post: 03-04-2010, 05:42 AM
  3. Checking value of one character in array
    By System_159 in forum C Programming
    Replies: 7
    Last Post: 01-17-2008, 05:05 PM
  4. Checking to see if a string is just a newline character
    By Beowolf in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2007, 09:29 PM
  5. Checking validity of input character
    By yank in forum C Programming
    Replies: 8
    Last Post: 04-16-2003, 02:42 AM

Tags for this Thread