In: Computer Science
/* There are following logical operators supported by C++ language.
1. && :- Called Logical AND operator. If both the
operands are non-zero, then condition becomes true.
example. (A && B) is false.
2. || :- Called Logical OR Operator. If any of the two operands
is non-zero, then condition becomes true.
example. (A || B) is true.
3. ! :- Called Logical NOT Operator. Use to reverses the logical
state of its operand. If a condition is true, then Logical NOT
operator will make false.
example. !(A && B) is true.
*/
// C++ program to illustrate logical operater while taking input from user
#include <iostream>
#include<string.h> // For string compare
using namespace std;
int main()
{
// Because we have yes as maximum length so charater
array of 3 element declared
char input[3];
// Ask question to user for an input
cout << "Do you wish to continue? ";
cin >> input;
/* Here we are using strcmp function in string.h for
cpmparing input with our given criateria
and logical or operator if any 1
condition is true then it gives output else gives error*/
// This if checks for yes condition
if( strcmp(input, "Y")==0 || strcmp(input, "Yes")==0 ||
strcmp(input, "YES")==0 )
cout << "\ncontinuing";
// This if condition check for no
condition
else if( strcmp(input, "N")==0 || strcmp(input, "No")==0 ||
strcmp(input, "NO")==0)
cout << "\nQuit";
// otherwise it comes
here
else
cout << "\nBad Input";
return 0;
}
/* output :-
1) Do you wish to continue? NO
Quit
2) Do you wish to continue? Y
continuing
3) Do you wish to continue? kjhgjh
Bad Input
*/