Question Regarding Boolean Please

Discussion in 'C Programming' started by r1ppx, Dec 27, 2017.

  1. r1ppx

    r1ppx

    Joined:
    Dec 27, 2017
    Messages:
    2
    Likes Received:
    0
    Hello,

    I just started teaching myself C++ from a very old book and I was hoping somebody could please help me out. I haven't gotten into functions yet.


    #include "stdafx.h"
    #include <iostream>
    using namespace std;


    int main()
    {
    float Dues;
    float AmountDue;

    cout << "Enter the amount due: ";
    cin >> Dues;


    cout << "Are the dues paid on time? (y/n) ";
    char yn;
    cin >> yn;


    bool Overdue; //True if overdue, false if on time
    Overdue = yn != 'y';

    if (Overdue)
    AmountDue = Dues * 1.10;
    else
    AmountDue = Dues;


    cout << AmountDue;
    cout << "\n\n";


    return 0;
    }


    I am very confused as to what's going on with the boolean variable Overdue, specifically how the heck do I know if it's set to true or false!?!

    First, I know that we have to assign Overdue to the variable yn:

    Overdue = yn

    Then this next part really confuses me because at this point Overdue can equal either a y or a n depending on the person's input.

    And now it's saying:

    != 'y'

    So overdue can equal y or n, BUT it doesn't equal (!=) y. So to me this looks like if overdue is set to n that means its now set to true because it cannot equal y (because of this: !=)?
     
    r1ppx, Dec 27, 2017
    #1
    1. Advertisements

  2. r1ppx

    r1ppx

    Joined:
    Dec 27, 2017
    Messages:
    2
    Likes Received:
    0
    UPDATE: I found the answer.
     
    r1ppx, Dec 27, 2017
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.