CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2015
    Posts
    1

    Logical AND operator help-beginner

    Hey guys, On my program I am trying to have an IF statement with two conditions to equal to true that it will cout the executable code. HOWEVER, when I run it, it does NOT go through the executable code, telling me that the conditions are evaluating to false. Any help on fixing this? (specifically this section: if(color==1 && donate == Y).

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <cstdlib>

    using namespace std;

    int main()
    {
    const double RED = .05;
    const double YELLOW = .10;
    const double BLUE = .15;
    const double ORANGE = .20;
    const double GREEN = .25;
    const int WHITE = 1;
    const double extraDiscount = .05;
    const double TAX = .0825;


    //variable declarations
    int color;
    string donate;
    double bookPrice;
    double finalCost;
    double discount;
    string Y;
    string N;


    cout << setprecision(2) << fixed;

    // Getting book and sticker information from the user
    cout << "What is the marked price on your book?" << endl;
    cin >> bookPrice;
    cout << "Please enter the corresponding number to the sticker color on ";
    cout << "your book:" << endl;
    cout << "1-Red, 2-Yellow, 3-Blue, 4-Orange, 5-Green, 6-White" << endl;
    cin >> color;
    cout << "Would you like to donate $10 to the Special Olympics?";
    cout << "(Y or N)" << endl;
    cin >> donate;
    cout << endl;


    if(color == 1 && donate == Y )
    {
    discount = (RED * bookPrice) + (bookPrice * extraDiscount);
    finalCost = (bookPrice - discount) + (TAX * (bookPrice - discount));

    cout << "You have selected the RED sticker with a ";
    cout << RED * 100 << "% discount." << endl; //converts to percentage
    cout << "Your choice to donate qualifies you for an";
    cout << " additional discount on this book." << endl;
    cout << endl;
    cout << "The marked price on your book is $" << bookPrice << endl;
    cout << "You will receive a discount of $";
    cout << discount << " on your book." << endl;
    cout << "The discounted price of the book is ";
    cout << bookPrice - discount<< endl;
    cout << "Tax is " << TAX * (bookPrice - discount) << endl;
    cout << "Final cost of the book is " << finalCost << endl;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Logical AND operator help-beginner

    Quote Originally Posted by zaizen View Post
    Hey guys, On my program I am trying to have an IF statement with two conditions to equal to true that it will cout the executable code. HOWEVER, when I run it, it does NOT go through the executable code, telling me that the conditions are evaluating to false. Any help on fixing this? (specifically this section: if(color==1 && donate == Y).
    ...
    Very easy!
    Either color is NOT 1, or donate is NOT equal Y.
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Logical AND operator help-beginner

    When posting code, please use code tags. As posted the code is not very readable. Go Advanced, select the formatted code and click '#'.

    donate is of type string. So the test should be
    Code:
    if(color == 1 && donate == "Y" )
    You don't need the variables Y and N.

    For more info on string literals see http://www.cplusplus.com/doc/tutorial/constants/
    Last edited by 2kaud; March 2nd, 2015 at 03:50 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Jul 2013
    Posts
    576

    Re: Logical AND operator help-beginner

    Quote Originally Posted by zaizen View Post
    if(color == 1 && donate == Y )
    In the above you test whether the variables donate and Y hold the same string value.

    You assign a value (a user input) to donate but Y has never been assigned a value. And since you probably want the variable Y to hold the string "Y" you can change the declaration of Y to

    Code:
    string Y = "Y";
    Now if donate is assigned a "Y" then donate==Y will be true since Y is also "Y".
    Last edited by razzle; March 2nd, 2015 at 06:01 AM.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Logical AND operator help-beginner

    In this case IMO Y should be declared const as its value isn't going to change.

    Code:
    const string Y = "Y";
    and similarly for string N.
    Last edited by 2kaud; March 2nd, 2015 at 07:19 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Logical AND operator help-beginner

    Code:
    if(color == 1 && donate == "Y" )
    Are you going to have similar tests for other colours with/without donate? If you are then there are much easier ways of coding this. Have you come across arrays/structs yet?

    What testing are you doing to check that the entered values for color and donate are valid? Are there values for bookPrice which can be entered but would be invalid (hint negative value)?
    Last edited by 2kaud; March 2nd, 2015 at 07:23 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured