使用结构的成员作为validation

我必须从excel文件中读取列,以便输出它的统计信息。 我想这要做到这一点,我必须先创build一个struct 。 这是我迄今为止所有的。

 #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; 

excel文件是Facebook类似计数和成员名称,ID和性别的列表。 我的问题是,如何使用if语句来validation数据,并找出类似的计数属于male还是female

任何帮助,将不胜感激。

编辑:使问题更清晰(我希望)。

比较string(实际上是以'\0'结尾的字符数组)的做法是为作业使用正确的函数。

这是一个使用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; } 

输出:

 Male looks male. Female looks female. 

我build议为了学习的目的进行实验。
例如尝试将结构中的string更改为“女性”和“男性”。
我做代码的方式在这种情况下会有一个启发性的结果。 🙂
(学分Idlle001)