In class we discussed Boolean operators (and, or , and not). One Boolean operator that is not included in C++ is the “exclusive or” often abbreviated as xor. The output of the a xor b is true if a is true or b is true, but not both. Write a program that does the following: Asks the user to type in t or f for the value of the first variable, designating either true or false. Ask again for the second variable. It then calculates the exclusive or of the two variables and outputs: “The exclusive or of those two inputs is _____, where the blank says either true or false, depending on the exclusive or outcome. Note if the person types in anything besides t or f the program should say invalid input please try again and allow the user to make another attempt.
Expert Answer
Answer:
#include<iostream> /* header file for standard input and output streams */
using namespace std;
int main() /* main() function definition starts here */
{
char c1,c2; /* variable declaration */
cout<<“Enter the truth value of first boolean variable (t or T for TRUE/f or F for FALSE):”; /* prompts the message */
cin>>c1; /* takes the character into c1 */
cout<<“Enter the truth value of second boolean variable (t or T for TRUE/f or F for FALSE):”; /* prompts the message */
cin>>c2; /* takes the character into c2 */
if((c1!=’t’&&c1!=’T’&&c1!=’f’&&c1!=’F’)||(c2!=’t’&&c2!=’T’&&c2!=’f’&&c2!=’F’)) /* if c1 is other than t/T/f/F or c2 is other than t/T/f/F */
cout<<“Invalid input”; /* prompts the message */
else /* otherwise */
{
if((c1==’T’||c1==’t’)&&(c2==’T’||c2==’t’)) /* if c1 is T/t and c2 is T/t */
cout<<“The Exclusive OR of “<<c1<<” and “<<c2<<” is FALSE”; /* prompts the message (FALSE) */
else if((c1==’F’||c1==’f’)&&(c2==’F’||c2==’f’)) /* if c1 is F/f and c2 is F/f */
cout<<“The Exclusive OR of “<<c1<<” and “<<c2<<” is FALSE”; /* prompts the message (FALSE) */
else if((c1==’F’||c1==’f’)&&(c2==’T’||c2==’t’)) /* if c1 is F/f and c2 is T/t */
cout<<“The Exclusive OR of “<<c1<<” and “<<c2<<” is TRUE”; /* prompts the message (TRUE) */
else if((c1==’T’||c1==’t’)&&(c2==’F’||c2==’f’)) /* if c1 is T/t and c2 is F/f */
cout<<“The Exclusive OR of “<<c1<<” and “<<c2<<” is TRUE”; /* prompts the message (TRUE) */
}
return 0;
} /* End of main() */
Output: