Hello! I'm very new to C++, I've only been learning for about a month. I posted this question earlier, but I made it very wordy and probably hard to understand.
This is just part of my program, but it's the part I'm having trouble with. Is there a way I can put all the cout statements together at the end instead of separated after every if/else if statement? I'm not sure if my question makes sense, I"m not quite sure how to word it...
double salesTaxTotal;//the total amount of sales Tax
double salesTaxPercent;//the percentage of sales tax
salesTaxPercent = .07;
double salesTaxHotDog;//the sales tax on a hot dog
double hotdogPrice;//the price of a hot dog
char h;//their answer to the question if they want a hot dog
hotdogPrice = 2.50;
salesTaxHotDog = hotdogPrice * salesTaxPercent;
char Y; char y;//yes
char N; char n;//no
cout << "Do you want a hot dog? Y for yes, N for no." << endl;
cin >> h;
if(h == 'Y' || h == 'y')
{
cout << "Hot dog: $" << fixed << showpoint << setprecision(2) << hotdogPrice <<endl;
salesTaxTotal = salesTaxHotDog;
cout << "Sales Tax: $" << salesTaxHotDog << endl;
}
elseif(h == 'N' || h == 'n')
{
cout << "No hotdog" << endl;
salesTaxHotDog = 0;
}
else
cout << "Illegal input!" << endl;
char d;//their answer to the question if they want a soft drink
char s;//their answer to what size soft drink they want
char M; char m;//medium soft drink
char L; char l;//large soft drink
double mediumPrice;//price of a medium soft drink
double largePrice;//price of a large soft drink
double salesTaxMedium;//the sales tax on a medium drink
double salesTaxLarge;//the sales tax on a large drink
mediumPrice = 2.50;
largePrice = 4.00;
salesTaxMedium = salesTaxPercent * mediumPrice;
salesTaxLarge = salesTaxPercent * largePrice;
cout << "Do you want a soft drink? Y for yes, N for no." << endl;
cin >> d;
if (d == 'Y' || d == 'y')
{
cout << "Do you want a medium or large soft drink? M for medium, L for large" << endl;
cin >> s;
if (s == 'M' || s == 'm')
{
cout << "Medium drink: $" << mediumPrice << endl;
salesTaxMedium = mediumPrice * salesTaxPercent;
cout << "Sales Tax on Medium Drink: $" << salesTaxMedium << endl;
}
elseif (s == 'L' || s == 'l')
{
cout << "Large soft drink: $" << largePrice << endl;
salesTaxLarge = largePrice * salesTaxPercent;
cout << "Sales Tax on Large Drink: $" << salesTaxLarge << endl;
}
}
elseif (d == 'N' || d == 'n')
cout << "No soft drink" << endl;
else
cout << "Illegal input!" << endl;
Something which every programmer learns, is that a data driven solution is always superior to a hardcoded one.
I would write out an example of what I mean, but as you said, you're a beginner, so some of the code you're not liable to understand.
However, if you really would appreciate a specific example, I can write out once I'm not so tired.