Hello imthekingbabyz,
You could also do jonnin's suggestion as:
if (s[0] == 'y' || s[0] == 'Y')
Do not count on the user entering what you want or thee way you want it. Most often you will be wrong or the user will find some way of doing it wrong. For something like this much better to stay with a single character.
You could also use:
if(std::tolower(c) == 'y')
You will need to include the header file "<cctype>" to use "std::tolower" or "std::toupper".
If you include the header file "<iomanop>" you can write:
std::cout << std::fixed << std::showpoint << std::setprecision(2);
and replace lines 18, 19 and 20. If you insist on using
using namespace std; // <--- Best not to use.
you can remove the "std::"s.
For you function:
1 2 3 4 5 6 7 8 9 10 11 12
|
double couponcode(double bill, char coupon)
{
double couponc;
if (coupon = 'yes')
{
cout << "please enter a single digit ";
cin >> couponc;
}
return(coupon);
}
|
First off the function says it is returning a "double", but the return statement,(Note: the () around "coupon" are not needed, is returning a "char". This may work, but it is not returning the correct variable or the percentage that is needed.
The if statement would work better in "main" not here. You should only use this function if it is needed.
Your instructions would lead one to believe that a coupon code will consist of more than one number and maybe it could even contain a letter. Not sure what you want to do with this.
Once you have a number you need something to deal with deducting the proper amount from the subtotal and return that back to main. If statements will work, but this would be a good place for a "switch".
Back in "main" I would consider changing "bill" to "subtotal" adding a variable "salesTax" and figure the sales tax in "main".
Your outout could look like this
With a coupon:
The number of item purchased: 1
The price of item purchased: 10
do you have a coupon? y or n: n
1 items at $10.00 each.
Subtotal $10.00
Tax 0.50
Coupon 1241 5.00
final bill, incuding tax, is $5.50
Press any key to continue . . .
|
Without a coupon:
The number of item purchased: 1
The price of item purchased: 10
do you have a coupon? y or n: n
1 items at $10.00 each.
Subtotal $10.00
Tax 0.50
Coupon 0.00
final bill, incuding tax, is $10.50
Press any key to continue . . .
|
For now this is hard coded, but easily changed to use variables.
Hope that helps,
Andy