why "Invalid Gender!"?
-
im having a problem here.. my program always output Invalid Gender! after I input the gender and status.. #include stdio.h> /*Ambus*/ int main() { char stat[10], gend[10]; int rCTR, mmCTR, fsCTR; clrscr(); mmCTR = 0; fsCTR = 0; for (rCTR = 1; rCTR <= 10; rCTR++) { JJ:printf("\n Record %d \n",rCTR); printf("Input your gender:"); scanf("%s",&gend); printf("Input your civil status:"); scanf("%s",&stat); if(gend == "female") { if(stat == "single") fsCTR++; else if(stat == "married"); else { printf("Invalid civil status!"); goto JJ; } } else if(gend == "male") { if(stat == "married") mmCTR++; else if(stat == "single"); else { printf("Invalid civil status!"); goto JJ; } } else { printf("Invalid gender!"); goto JJ; } } printf("Single females=%d, Married males=%d, Number of Records=%d.", mmCTR, fsCTR, rCTR-1); return 0; }
-
im having a problem here.. my program always output Invalid Gender! after I input the gender and status.. #include stdio.h> /*Ambus*/ int main() { char stat[10], gend[10]; int rCTR, mmCTR, fsCTR; clrscr(); mmCTR = 0; fsCTR = 0; for (rCTR = 1; rCTR <= 10; rCTR++) { JJ:printf("\n Record %d \n",rCTR); printf("Input your gender:"); scanf("%s",&gend); printf("Input your civil status:"); scanf("%s",&stat); if(gend == "female") { if(stat == "single") fsCTR++; else if(stat == "married"); else { printf("Invalid civil status!"); goto JJ; } } else if(gend == "male") { if(stat == "married") mmCTR++; else if(stat == "single"); else { printf("Invalid civil status!"); goto JJ; } } else { printf("Invalid gender!"); goto JJ; } } printf("Single females=%d, Married males=%d, Number of Records=%d.", mmCTR, fsCTR, rCTR-1); return 0; }
-
im having a problem here.. my program always output Invalid Gender! after I input the gender and status.. #include stdio.h> /*Ambus*/ int main() { char stat[10], gend[10]; int rCTR, mmCTR, fsCTR; clrscr(); mmCTR = 0; fsCTR = 0; for (rCTR = 1; rCTR <= 10; rCTR++) { JJ:printf("\n Record %d \n",rCTR); printf("Input your gender:"); scanf("%s",&gend); printf("Input your civil status:"); scanf("%s",&stat); if(gend == "female") { if(stat == "single") fsCTR++; else if(stat == "married"); else { printf("Invalid civil status!"); goto JJ; } } else if(gend == "male") { if(stat == "married") mmCTR++; else if(stat == "single"); else { printf("Invalid civil status!"); goto JJ; } } else { printf("Invalid gender!"); goto JJ; } } printf("Single females=%d, Married males=%d, Number of Records=%d.", mmCTR, fsCTR, rCTR-1); return 0; }
You are comparing a character array (a pointer) with a pointer to a constant string:
if(gend == "female")
Those pointers will never be identical. If you want to compare strings (the content pointed to by the pointers), you must compare each element (character) of the strings. There is the strcmp[^] C standard library function to do this:
if (!strcmp(gend, "female"))
;P
-
im having a problem here.. my program always output Invalid Gender! after I input the gender and status.. #include stdio.h> /*Ambus*/ int main() { char stat[10], gend[10]; int rCTR, mmCTR, fsCTR; clrscr(); mmCTR = 0; fsCTR = 0; for (rCTR = 1; rCTR <= 10; rCTR++) { JJ:printf("\n Record %d \n",rCTR); printf("Input your gender:"); scanf("%s",&gend); printf("Input your civil status:"); scanf("%s",&stat); if(gend == "female") { if(stat == "single") fsCTR++; else if(stat == "married"); else { printf("Invalid civil status!"); goto JJ; } } else if(gend == "male") { if(stat == "married") mmCTR++; else if(stat == "single"); else { printf("Invalid civil status!"); goto JJ; } } else { printf("Invalid gender!"); goto JJ; } } printf("Single females=%d, Married males=%d, Number of Records=%d.", mmCTR, fsCTR, rCTR-1); return 0; }
I wonder: what IDE / compiler are you using? Isn't the debugging possible? :confused:
-
You are comparing a character array (a pointer) with a pointer to a constant string:
if(gend == "female")
Those pointers will never be identical. If you want to compare strings (the content pointed to by the pointers), you must compare each element (character) of the strings. There is the strcmp[^] C standard library function to do this:
if (!strcmp(gend, "female"))
;P
ohh.. i see.. thank you very much :3
-
I wonder: what IDE / compiler are you using? Isn't the debugging possible? :confused:
it is possible.. btw im just a beginner :3