easy quick question - validation

Posted on 2005-03-17
Medium Priority
Last Modified: 2010-04-01
Hello Experts,

Can somebody explain me on beginner level how should I rewrite this code? I'm trying to do validation for char variable with while loop.

do {
cout << "Please enter your sex\n";
cout << "\t M - Male\n";
cout << "\t F - Female\n";
cout << " ==> ";
cin >> s;
} while ( s != 'f' );

this works but just for 'f' but when I add "or"

do {
cout << "Please enter your sex\n";
cout << "\t M - Male\n";
cout << "\t F - Female\n";
cout << " ==> ";
cin >> s;
} while ( s != 'f' || s != 'F' || s != 'm' || s != 'M');


Question by:Luxana
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Assisted Solution

AlexFM earned 300 total points
ID: 13572135
while (  ! (s == 'f' || s == 'F' || s == 'm' || s == 'M') );

Accepted Solution

zzray earned 600 total points
ID: 13572226
what is also equivalent to the above ans is  while (  s != 'f' && s != 'F' && s != 'm' && s != 'M' );
i.e replacing all your ors with ands.

The reson why your condition did not work is because you are saying that if
s is not 'f' or s is not 'F'or sis not 'm or s is not 'M' continue  looping.. but s can only take on one value 'f' or 'F' 'm' or 'M' so if its any of the four or none of the four vaues the expression evaluates to 1 and remains in the loop.


Expert Comment

ID: 13572707
simply change the || to && .....

while ( s!='f' && s!='F' && s!='m' && s!='M')

Assisted Solution

r_a_j_e_s_h earned 100 total points
ID: 13572713
it will work till the entered text is not equal to any one of the character
LVL 10

Author Comment

ID: 13572932
thanks you all

I'll accept zzray answer because of his explanation and rest of answers assist.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question