if statement/logical operators

Determine whether the user qualifies for discount movie tickets.
Use the if-else structure to decide which of the following
two messages to print: Congratulations!
You qualify for discounted movie tickets.

Sorry.
You must be a student or a senior citizen to receive
discounted movie tickets.

Discount Ticket eligibility (at least one of these)
- Student
- Senior Citizen (65 or older)
Use just ONE of the logical operators below in the expression
to choose the appropriate message.

Logical AND: &&
Logical OR: ||

Note: Both operators can be made to work.
Note: You MUST use a compound logic expression, only ONE
if and ONE else are allowed
Note: Do NOT worry about situations where the user doesn't enter Y, y, N, or n.

This is what I wrote, but still has a logic error when I test for both being 'n'.

Test #1
==========================================
Answer the following questions
with either yes or no (y or n):

Are you a student? y
Are you 65 or older? y

Congratulations!
You qualify for discounted movie tickets.

Press any key to continue . . .

Test #2
==========================================
Answer the following questions
with either yes or no (y or n):

Are you a student? y
Are you 65 or older? N

Congratulations!
You qualify for discounted movie tickets.

Press any key to continue . . .

Test #3
==========================================
Answer the following questions
with either yes or no (y or n):

Are you a student? N
Are you 65 or older? y

Congratulations!
You qualify for discounted movie tickets.

Press any key to continue . . .

Test #4
==========================================
Answer the following questions
with either yes or no (y or n):

Are you a student? n
Are you 65 or older? N

Sorry.
You must be a student or a senior citizen to receive discounted movie tickets.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #include <iostream>
  #include <cctype>
  using namespace std;

  int main()
{
    char studentAnswer;        // Currently a student? Y or N
    char seniorCitizenAnswer;  // Are 65 or older? Y or N

    cout << "Answer the following questions" << endl
         >> "with either yes or no (y or n):"<< endl
         << endl;

    cout << "Are you a student?;
    cin >> studentAnswer;

    studentAnswer = tolower(studentAnswer);

    cout << "Are you 65 or order? ";
    cin >> seniorCitizenAnswer;

    seniorCitizenAnswer = tolower(seniorCitizenAnswer);

    cout << endl;

    // THIS IS WHAT I WROTE
    if(student == 'y' || seniorCitizenAnswer >= 65)
    cout << "Congratulations! \n"
         << "You qualify for discounted movie tickets. ";
    else
    cout << "Sorry.\n
         << "You must be a student or a senior citizen to receive discounted 
              movie ticket. ";

    return 0;
}
Last edited on
seniorCitizenAnswer … if you type 'n' in there you get the value for the char 'n' which is 110 which is >= 65.

either check for seniorCitizenAnswer == 'y' or do something else entirely.
Last edited on
Ahhhh I feel so stupid now...SeniorCitizenAnswer is a char, so idk why i was comparing it with a number. You helped me realized my mistake. thanks!
closed account (E0p9LyTq)
You have typos in your code that prevent it from compiling:

line 11 is using the extraction operator (>>) instead of the insertion operator (<<)

line 14 is missing the ending quotes (")

lines 32-33 are missing quotes (")

Once those are fixed your if statement is incorrect. You are checking for a decimal value for your senior citizen answer instead of a char value.

While this is possible the number you are checking for is the 'A' character. 'y' or 'n' will always be greater than or equal to 'A'.

if (studentAnswer == 'y' || seniorCitizenAnswer == 'y')
char IS a number, but its not the number you thought it was. Its an integer type with a 1 byte width, and when used for text, is using a lookup table of the characters. Again, n is 110, so its a perfectly legit number :)
Topic archived. No new replies allowed.