Logical AND operator help.

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)

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  #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;
}
if(color == 1 && donate == Y )

variable Y is being used without being initialized,
to prevent such mistakes in future consider setting up your compiler to threat warnings as errors, and then compile with warning level 3 or 4.
Last edited on
Excuse my ignorance, but what does that mean? "Variable Y is being used without being initialized?
I defined it, and its being entered into cin.

Thanks
1
2
string Y; // this variable is not initialized
string Y = "Y"; // this variable is initialized 


in your code you created 'Y' but it's blank, that's why if(color == 1 && donate == Y ) is threated as false, because donate == Y is same 'Y' == "" which is false.
ok so Y isnt associated with anything. How could that be fixed? something like:

Y=1? 1 being the true value.
which would mean it would have to be defined at the top as bool Y, right?

Thanks again for your input
ok so Y isnt associated with anything.

that's right, it's empty, you need to ask user to input some value

for example:

1
2
cout << "input something into Y";
cin >> Y;


it depends on what 'Y' is supposed to be of course.
Last edited on
Wouldnt that be:

1
2
3
cout << "Would you like to donate $10 to the Special Olympics?";
cout << "(Y or N)" << endl;
cin >> donate;
In which donate would be a Y or N entered in by the user
yes, but you need ot initialize your variable before using them in that case:

for example modify your code like this:

1
2
string Y = "Y";
string N = "N";


better approach would be to not use these variable at all and just use chars:
if(color == 1 && donate == 'Y' )
Last edited on
Im getting the error code:

"No match for 'operator==' in 'donate == 'Y''
Ah, I think I got it.

I did if(color == 1 && donate == "Y") //With double quotes around Y.

That seemed to accept it and now seems so silly and easy. Thanks for the input and help!
Topic archived. No new replies allowed.