Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. why "Invalid Gender!"?

why "Invalid Gender!"?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ambus Dondon
    wrote on last edited by
    #1

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

    L J V 3 Replies Last reply
    0
    • A Ambus Dondon

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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      if(gend == "female")

      That is not a valid way to compare strings in C. You need to use one of the string compare functions: see strcmp, wcscmp, _mbscmp[^]. It is also a bad idea to use the goto statement, use loops instead.

      1 Reply Last reply
      0
      • A Ambus Dondon

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

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        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

        A 1 Reply Last reply
        0
        • A Ambus Dondon

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

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #4

          I wonder: what IDE / compiler are you using? Isn't the debugging possible? :confused:

          A 1 Reply Last reply
          0
          • J Jochen Arndt

            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

            A Offline
            A Offline
            Ambus Dondon
            wrote on last edited by
            #5

            ohh.. i see.. thank you very much :3

            1 Reply Last reply
            0
            • V Victor Nijegorodov

              I wonder: what IDE / compiler are you using? Isn't the debugging possible? :confused:

              A Offline
              A Offline
              Ambus Dondon
              wrote on last edited by
              #6

              it is possible.. btw im just a beginner :3

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups