0

I have to read columns from an excel file so that I could output a statistic of it. I figured that to do this, I have to create a struct first. This is all I have so far.

#include <stdio.h>
#include <stdlib.h>


    /* Structure to store all the data that is required */
    typedef struct{
        char* id;
        int entryCount;
        int like;
        char gender[7];
    } statistics;

The excel file is a list of facebook like count and member name, ID and gender. My question is, how can I use an if statement to validate the data and find out if the like count belongs to the male or female gender?

Any help would be appreciated.

Edit: Made question clearer (I hope).

Noob
  • 25
  • 3
  • 8
  • There is a bit more code in addition to `typedef`s and a `if` statement to read the stuff from an excel file ... perhaps you would `strcmp` the `gender` against `"male"` or `"female"` - but notice that `char gender[6]` is too small to fit the `"female"` + null terminator. – Antti Haapala -- Слава Україні Jun 25 '17 at 17:18
  • Your question is not so clear. What exactly is your goal after instantiating a struct per row in the table? – SHG Jun 25 '17 at 17:19
  • 1
    A side note, `gender[6]` will not contain `female` when you count the null character '\0' that will be appended at the end. – sjsam Jun 25 '17 at 17:19
  • @AnttiHaapala Yes, I know that I need fopen and stuff to get the info from an excel file, but what I need now is to validate the information so that I know it belongs to either male, or female. – Noob Jun 25 '17 at 17:20
  • @SHG After I have a struct, I can find the member of the struct like the like count from the excel file? – Noob Jun 25 '17 at 17:21
  • Is your question how to find out whether `gender` contains "male", "female" or neither? – Yunnosch Jun 25 '17 at 17:21
  • @sjsam Thanks for the info, I'll change it to gender[7] instead. – Noob Jun 25 '17 at 17:21
  • @Yunnosch Yes that is exactly what I am asking. I want to know if we can just use an if statement for it – Noob Jun 25 '17 at 17:22
  • Of course you can use an `if` statement. How else can you check if some string is equal/contains some other string? – SHG Jun 25 '17 at 17:26
  • @shg I know you can use an if statement, I'm just not sure of what to put inside the brackets of the if statment – Noob Jun 25 '17 at 17:27
  • put `if (!strcmp(struct.gender,"female")) printf("female") // else whatever` – Abr001am Jun 25 '17 at 17:29
  • 1
    @Idle001 `strncmp` is preffered. Safer! – SHG Jun 25 '17 at 17:31
  • 1
    @Noob You're bigger concerns here should be how to parse the excel file. One easy way to do that would be to use a excel to csv parser to read the file, write to a csv file and then use a similar parsert o convert the output file to excel. – sjsam Jun 25 '17 at 17:31
  • @sjsam Thanks for the information, but I do not know how to do that. – Noob Jun 25 '17 at 17:34
  • @Idle001 Thank you for the answer. I will try it. – Noob Jun 25 '17 at 17:35
  • @SHG case sensitivity ? – Abr001am Jun 25 '17 at 17:42
  • @Idle001 No, it's because of non-NULL-terminated strings. Read [this](https://stackoverflow.com/a/30190652/6032010). – SHG Jun 25 '17 at 18:38

1 Answers1

1

The way to make an if for comparing strings (actually arrays of characters, ending in '\0') is to use the correct function for the job.

Here is an example using strncmp(...).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/* Structure to store all the data that is required */
typedef struct{
    char* id;
    int entryCount;
    int like;
    char gender[7];
} statistics;

int main(void)
{
    statistics ExampleMale={"1", 2, 0, "male"};
    statistics ExampleFemale={"0", 1, 0, "female"};

    // Whatever you do to fill your structs is simulated by init above.
    if(strncmp(ExampleMale.gender,"male", 7))
    {
        printf("Male looks female.\n");
    } else
    {
        printf("Male looks male.\n");           
    }

    if(strncmp(ExampleFemale.gender,"female",7))
    {
        printf("Female looks male.\n");
    } else
    {
        printf("Female looks female.\n");           
    }

    return 0;

}

Output:

Male looks male.
Female looks female.

I recommend to experiment, for learning purposes.
Try for example to change the strings inside the structs to "Female" and "Male".
The way I made the code will have an enlightening result in that case. :-)
(Credits to Idlle001)

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Thanks for the answer. So the `statistics ExampleMale={"1", 2, 0, "male"};` is the data that I enter in? How do I get the statistics from a file using fopen? Also, what does the 7 mean in the if statement? – Noob Jun 25 '17 at 17:47
  • Yes it simulates the data you read in any method you like. Get it by using fopen() , yes, but followed up by using functions for reading from a file (http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html). 7 is the third parameter to strncmp(), read the spec (e.g. http://en.cppreference.com/w/c/string/byte/strncmp) and maybe compare to the other numbers in your code. ;-) – Yunnosch Jun 25 '17 at 17:50
  • Thanks for the sources. If 7 is the number of characters to compare, shouldn't the first `if` statement be not equal to 7? – Noob Jun 25 '17 at 17:56
  • 7 is the maximum number of characters to compare. It is mostly useful for avoiding the function running amok through all memeory. It can also be used to only compare the first few characters of a longer string, if that is logically desirable. Using 7 includes the `'\0'` by the way, which could be considered optional in this case, where things like "femaleaux" are out of scope. – Yunnosch Jun 25 '17 at 18:00