Boolean, yes or no

I'm trying to write a program that will read yes or no responses using boolean (bool student, bool disabled), but I have no idea how to go about doing this, I tried using switch but it doesn't work, the way I'm doing it is incorrect. Don't know what else to try, any help will be appreciated.


My code:

#include <iostream>
using namespace std;

struct sPassenger{ int age; bool student; bool disabled; };
int main()
{
int age; bool student; bool disabled;

cout << " Please enter your age " << endl;
cin >> age;


cout << " Are you disabled (Y/N)? " << endl;
cin >> disabled;
switch (disabled)
{
case 0 : true;
case 1 : false;
}

cout << " Are you a Student (Y/N)? " << endl;
cin >> student;
switch (student)
{
case 0 : true;
case 1 : false;
}

if ( age > 12 && age < 65) age = 0;
if ( age >= 65) age = 1;
if ( age <= 12) age = 2;

cout << " age is " << age << endl;


return (0);
}
You ask the user to enter Y/N, so they will input a char. Hence you need to use cin >> to read a char variable. Only after that do you need to be concerned with setting the boolean.
Can you explain how to set the boolean.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char answer;
cout << " Are you disabled (Y/N)?\n";
cin >> answer;

// could do answer = towlower( answer )
// so we don't have to worry about case

switch( answer )
{
case 'Y': case 'y': 
    disabled = true;
    break;

case 'N': case 'n':
    disabled = false;
    break;
}
Thank you.

Can it also be done like this?
1
2
3
4
5
6
7
8
9
10
11
cout << " Are you disabled (Y/N)? " << endl;
	char answer1;
	cin >> answer1;
	if (answer1 == 'Y' )
		{
		bool disabled = true;
		}
	if (answer1 == 'N' )
		{
		bool disabled = false;
		}
Yes it can, however, in Line 6 & 10, you are declaring and defining a local variable "disabled" and not assigning to main's "disabled" variable.

You could also do this to shorten it even further.
disabled = (answer1 == 'Y');
If (answer1 == 'Y') is true, then disabled will be true also. Otherwise it's false.

You may want to check for invalid input.
What if the user doesn't enter 'Y' or 'N'?
What if the user enters 'y' or 'n'?
Topic archived. No new replies allowed.