473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. getstudent_detail()
  2.  
  3.               {
  4.  
  5.                  do{
  6.  
  7.                      fscanf(sfile,"%i %[^/]%*c %[^/]%*c %c %d%*c %s", &ID, Course, Name, &Gender, &Age, Enrollment_Date);
  8.  
  9.                     }while (!feof(sfile));
  10.                      ID += 1;
  11.  
  12.                  }
  13.                  printf("Course ID: ");
  14.                  scanf(" %s", &Course);
  15.                  printf("Student ID: %04i", ID);
  16.                  printf("\nName: ");
  17.                  fflush(stdin);
  18.                  gets(Name);
  19.                  printf("Gender: ");
  20.                  scanf(" %c", &Gender);
  21.                    if ( getchar()!='m'||getchar()!='f')
  22.                         printf(" Please enter 'm' for Male or 'f' for Female :");
  23.                       return(0);
  24.                  printf("Age: ");
  25.                  scanf("%d", &Age);
  26.                  printf("Enrollment_Date in DD-MM-YY format: ");
  27.                  scanf("%s", &Enrollment_Date);
  28.                  printf("\nRecord saved!");
  29.                  sleep(3);
  30.                  fprintf(sfile,"%i %s/ %s/ %c %d %s\n", ID, Course, Name, Gender, Age,Enrollment_Date);
  31.                  fclose(sfile);
  32.                 }
May 14 '10 #1
8 6386
newb16
687 512MB
You already read it into Gender, why you need call getchar two more times?
May 14 '10 #2
sorry this is when I was changing code after my frustrations
the actual code used is
Expand|Select|Wrap|Line Numbers
  1. if ( Gender !='m'||Gender !='f')
  2. printf(" Please enter 'm' for Male or 'f' for Female :");
  3. return(0);
May 14 '10 #3
newb16
687 512MB
And what Gender equals to before comparison?
May 14 '10 #4
@faefaeus
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
May 14 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. if ( Gender !='m'||Gender !='f')
  2. 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.
May 14 '10 #6
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)
May 14 '10 #7
donbock
2,426 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. ...
  2. scanf(" %c", &Gender);
  3. if ( Gender!='m'&&Gender!='f')
  4. printf(" Please enter 'm' for Male or 'f' for Female :");
  5. return(0);
  6. ...
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.
Expand|Select|Wrap|Line Numbers
  1. ...
  2. scanf(" %c", &Gender);
  3. if ( Gender!='m'&&Gender!='f')
  4.    {
  5.    printf(" Please enter 'm' for Male or 'f' for Female :");
  6.    return(0);
  7.    }
  8. ...
May 14 '10 #8
Thank a lot Donbock,
It now works like magic.
I have even been able to do more validations
May 15 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

38
by: Kai Jaeger | last post by:
I am playing with setting font sizes in CSS using em as unit of measurement. All seems to be fine. Even Netscape Navigator shows the characters very similar to IE, what is not the kind if px is...
1
by: Neo | last post by:
I have a field named "Male" & this is of SQL BIT data type (SQL Server). How can i bind this field to a check box? I noticed that this field can be NULL, 0 or 1. But i'm having trouble binding...
22
by: campbellbrian2001 | last post by:
Thanks in Advance! ... I have two textboxes: 1 is visible (and gets its value based on the invisible textbox and displays either "Male" or "Female", and needs to display either male of female based...
11
by: TokyoJ | last post by:
I run a small camp in Alaska for kids and my director is asking for a web form. Could someone please have a look and offer some advice on where I'm making mistake(s)? I'm using the RegExp function...
5
by: elia | last post by:
Hello, I use php and javascript on a form. My validate script doesn't work with radio button. What's wrong? I want to be sure that one of the button is press. M or F I get on my first page: ...
1
by: huhuhuhu | last post by:
i am trying to validate radiobutton n checkbox but to no avail. this is the code that i m using to validate radiobutton>> <script language="JavaScript"> function checkForm() { var cgender...
4
by: CF FAN | last post by:
I want to validate this two checkbox variables on an action page and also an update page <cfinput type="checkbox" name="gender"checked> </label></td> <td>Female</td> ...
13
by: Missionary2008 | last post by:
I'm using Access 2007 with Vista. I'm trying to figure out how to write a query to take information in the fields GENDER, RETIREMENT DATE, and REGION to get a count of the number of missionaries that...
3
by: dixiebuyer | last post by:
I have a project I am porting to the Web from stand alone Access and Word with VB. I have been pleased with LAMP so far, but this one part of the project is killing me! Basically what I want to do...
5
by: ghjk | last post by:
I have a web site with several web pages. They are having different number of variables. eg: first page having 4 variables in the form(Name, Phone, ID, Gender) second page having 10 variables in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.