c++ checkout

I need help with an assignment and I need help. I need to give the option to take in more items based on the user's input. Return the total to main. and also • One function that takes in coupon codes as integers – if the code ends in a 1, the total is 50% off, if the code ends in a 5, the total is $5 off. Return that amount and output it.

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
#include<iostream>
using namespace std;
double totalCost(int numberParameter, double priceParameter);
double couponcode(double bill,char coupon);
int main()
{
	
	double price, bill,ctotal;
	char coupon;
		int number;

		cout << "The number of item purchased:";
		cin >> number;
		cout << "The price of item purchased:";
		cin >> price;
				cout << "do you have a coupon? yes or  no: ";
		cin >> coupon;
			cout.setf(ios::fixed);
			cout.setf(ios::showpoint);
			cout.precision(2);
			bill = totalCost(number, price);
			ctotal = couponcode(bill,coupon);
			cout << number << " items at "
				<< "$" << price << " each. \n"
				<< "final bill, incuding tax, is $" << bill << endl;
		
		system("pause");
		
		return 0;
}

double totalCost(int numberParameter, double priceParameter)
{
	const double TAXRATE = 0.05;
	double subtotal;
	subtotal = priceParameter * numberParameter;
	return (subtotal + subtotal * TAXRATE);
}

double couponcode(double bill,char coupon)
{
	double couponc;
	if (coupon = 'yes')
	{
		cout << "please enter a single digit ";
		cin >> couponc;
	}
return(coupon);
} 
> if (coupon = 'yes')
1. coupon is a char, so you're limited to 'y' or 'n' (or any other single character).
2. Use == for comparing, = is assignment.

> cout << "please enter a single digit ";
> cin >> couponc;
The prompt is mis-leading, when you're reading into a double.
how would I make the char into 3 characters?
most people would rather type 1 letter for y/n but you could do this:
#include<string> //at the top, of course
string s;
cin >> s;
if (s == "yes") //case matters: "Yes" will not work, "YES" will not work... etc.

a recap of minor important details:
== for comparison
"many" for strings
'c' for single characters


Last edited on
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
Topic archived. No new replies allowed.