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>
usingnamespace std;
int main()
{
constdouble RED = .05;
constdouble YELLOW = .10;
constdouble BLUE = .15;
constdouble ORANGE = .20;
constdouble GREEN = .25;
constint WHITE = 1;
constdouble extraDiscount = .05;
constdouble 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;
}
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.
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 whyif(color == 1 && donate == Y ) is threated as false, because donate == Y is same 'Y' == "" which is false.