Validate gender

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • faefaeus
    New Member
    • May 2010
    • 5

    #1

    Validate gender

    Hi,

    I am trying to validate against gender in my code.
    The function below accepts student details, part of its function is to verify that the correct lettter is enterd for gender 'm' or 'f'
    It llooks like the program does not understand input as it treats all character inputs the same.any characters causes the function to abort.
    You help will be apprecaited
    Code:
    getstudent_detail()
    
                  {
    
                     do{
    
                         fscanf(sfile,"%i %[^/]%*c %[^/]%*c %c %d%*c %s", &ID, Course, Name, &Gender, &Age, Enrollment_Date);
    
                        }while (!feof(sfile));
                         ID += 1;
    
                     }
                     printf("Course ID: ");
                     scanf(" %s", &Course);
                     printf("Student ID: %04i", ID);
                     printf("\nName: ");
                     fflush(stdin);
                     gets(Name);
                     printf("Gender: ");
                     scanf(" %c", &Gender);
                       if ( getchar()!='m'||getchar()!='f')
                            printf(" Please enter 'm' for Male or 'f' for Female :");
                          return(0);
                     printf("Age: ");
                     scanf("%d", &Age);
                     printf("Enrollment_Date in DD-MM-YY format: ");
                     scanf("%s", &Enrollment_Date);
                     printf("\nRecord saved!");
                     sleep(3);
                     fprintf(sfile,"%i %s/ %s/ %c %d %s\n", ID, Course, Name, Gender, Age,Enrollment_Date);
                     fclose(sfile);
                    }
    Last edited by Frinavale; May 14 '10, 08:17 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    You already read it into Gender, why you need call getchar two more times?

    Comment

    • faefaeus
      New Member
      • May 2010
      • 5

      #3
      sorry this is when I was changing code after my frustrations
      the actual code used is
      Code:
      if ( Gender !='m'||Gender !='f')
      printf(" Please enter 'm' for Male or 'f' for Female :");
      return(0);
      Last edited by Frinavale; May 14 '10, 08:17 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        And what Gender equals to before comparison?

        Comment

        • faefaeus
          New Member
          • May 2010
          • 5

          #5
          Originally posted by faefaeus
          Hi,

          I am trying to validate against gender in my code.
          The function below accepts student details, part of its function is to verify that the correct lettter is enterd for gender 'm' or 'f'
          It llooks like the program does not understand input as it treats all character inputs the same.any characters causes the function to abort.
          You help will be apprecaited

          getstudent_deta il()

          {

          do{

          fscanf(sfile,"% i %[^/]%*c %[^/]%*c %c %d%*c %s", &ID, Course, Name, &Gender, &Age, Enrollment_Date );

          }while (!feof(sfile));
          ID += 1;

          }
          printf("Course ID: ");
          scanf(" %s", &Course);
          printf("Student ID: %04i", ID);
          printf("\nName: ");
          fflush(stdin);
          gets(Name);
          printf("Gender: ");
          scanf(" %c", &Gender);
          if ( getchar()!='m'| |getchar()!='f' )
          printf(" Please enter 'm' for Male or 'f' for Female :");
          return(0);
          printf("Age: ");
          scanf("%d", &Age);
          printf("Enrollm ent_Date in DD-MM-YY format: ");
          scanf("%s", &Enrollment_Dat e);
          printf("\nRecor d saved!");
          sleep(3);
          fprintf(sfile," %i %s/ %s/ %c %d %s\n", ID, Course, Name, Gender, Age,Enrollment_ Date);
          fclose(sfile);
          }
          Please excuse my ignorance I did not assign a value to the Gender variable
          I assumed that scanf(" %c", &Gender); would assign the value that was typed in

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Code:
            if ( Gender !='m'||Gender !='f')
            printf(" Please enter 'm' for Male or 'f' for Female :");
            In this code if Gender is x then it is not equal to m and the statement is true.
            In this code if Gender is m then it is not equal to f and the statement is true.
            In this code if Gender is f then it is not equal to m and the statement is true.

            Like the statement is true all the time.

            You need to code if Gender is not m AND not f instead of coding OR.

            Comment

            • faefaeus
              New Member
              • May 2010
              • 5

              #7
              Thank a lot for the response

              It make a lot of sense, so it would be :

              if ( Gender !='m'&&Gender !='f')

              the funny thing is it still treats all character inputs as true and executes the return(0)

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Code:
                ...
                scanf(" %c", &Gender);
                if ( Gender!='m'&&Gender!='f')
                printf(" Please enter 'm' for Male or 'f' for Female :");
                return(0);
                ...
                Only the single statement immediately following the if statement is conditional. That's the printf. If you want the return to also be conditional then you need to enclose both statements in braces. It also helps to indent your code -- but remember, the indentation is only for you. The compiler does not pay any attention to indentation.
                Code:
                ...
                scanf(" %c", &Gender);
                if ( Gender!='m'&&Gender!='f')
                   {
                   printf(" Please enter 'm' for Male or 'f' for Female :");
                   return(0);
                   }
                ...

                Comment

                • faefaeus
                  New Member
                  • May 2010
                  • 5

                  #9
                  Thank a lot Donbock,
                  It now works like magic.
                  I have even been able to do more validations

                  Comment

                  Working...