Need help with my C++ program please.

So everything checks out for the most part. The problem comes when I press N. I tell the user say to enter in N when the user is finished. When the user enters in N, nothing happens. The user has to press N again or some other key for the program to continue. How do I make it so that doesn't happen? I want the program to move on to the calculations and finish as soon as the user enters in N.

Also, this program is supposed to take $2 off for every $20 spent. I have no idea how to apply that discount. I know that it is supposed to go right after the total sales are added, but I don't know the math behind it. I know how to find the percentage of a number, but how do I make the program do it for every $20 spent?

I am using MS Visual Studios 2015.

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
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
	char product_type;
	int short quantity_sold;
	int short quantity_soldA = 0, quantity_soldB = 0, quantity_soldC = 0;
	double totalA = 0.0, totalB = 0.0, totalC = 0.0;
	double a = 1.99, b = 2.99, c = 3.99;
	double total_sales;

	cout << "Welcome to Mail Mart! We sell three products: A, B, and C. " << endl << endl;
	cout << "We offer a discount of 10% for every $20 you spend! " << endl << endl;


	do
	{
		cout << "Enter the product type and quantity separated by a space (enter in N when you are finished): ";
		cin >> product_type >> quantity_sold;

		cout << setprecision(2) << fixed << showpoint;

		switch (product_type)
		{
		case 'A': quantity_soldA += quantity_sold;
			totalA = quantity_soldA * a;
			break;
		case 'B': quantity_soldB += quantity_sold;
			totalB = quantity_soldB * b;
			break;
		case 'C': quantity_soldC += quantity_sold;
			totalC = quantity_soldC * c;
			break;
		case 'N':
			break;
		default: cout << "You must enter a valid product type! " << endl;
		}
	} while (product_type != 'N');


	total_sales = totalA + totalB + totalC;


	cout << "The total sales for product A are $" << totalA << endl << endl;
	cout << "The total sales for product B are $" << totalB << endl << endl;
	cout << "The total sales for product C are $" << totalC << endl << endl;
	cout << "The total sales of all products are $" << total_sales << endl << endl;


	//cout << "Discount: ";
	//cout << "Amount due: ";
	cout << "Thank you for shopping at Mail Mart! Enjoy the rest of your day! " << endl << endl;
	return 0;
}.
So the problem you had entering N stemmed from both the quantity and product_type input occurring before your switch statement did its thing. So you enter N to checkout, but it has to get input for the quantity before it can go to the "N case". I just moved the input for quantity down into the individual cases. This makes it easier for the user too since typing the product, then a space, and then the quantity is kind of clunky. Instead, it plainly tells them when to add the quantity, cutting down on possible input errors. I also made it so the user could enter in either upper or lower case letters. Not really too important, but helpful.

For the discount there are a few ways you could do it, like a for loop, but the easiest I could think of is looking at it as division. How many times does 20 go into the total? If it's a 64 dollar total, $20 goes into it 3.2 times. Since we're not using decimals for the discount, I used floor() to round down to the nearest integer, or 3. Multiply that by 2 since you get $2 off for every time you spend $20, and you get a discount of $6. Subtract it from your total to get your total cost.

Full disclosure: I've only been programming for roughly a month or so, so I guarantee there are more sexy methods, but the program should run as you intend it to now.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;


int main()
{
	char product_type;
	int short quantity_sold;
	int short quantity_soldA = 0, quantity_soldB = 0, quantity_soldC = 0;
	double totalA = 0.0, totalB = 0.0, totalC = 0.0;
	double a = 1.99, b = 2.99, c = 3.99;
	double total_sales, discount, net;

	cout << "Welcome to Mail Mart! We sell three products: A, B, and C. " << endl << endl;
	cout << "We offer a discount of 10% for every $20 you spend! " << endl;


	do
	{
		cout << "\nWhen you are finished, please enter N to check out."
                     << "\nPlease enter the product you would like to purchase by entering A, B, or C: ";
		cin >> product_type;

		cout << setprecision(2) << fixed << showpoint;

		switch (product_type)
		{
		case 'A':
		case 'a':	cout << "\nPlease enter how many you would like to purchase: ";
					cin >> quantity_sold;			
					quantity_soldA += quantity_sold;
					totalA = quantity_soldA * a;
					break;
		case 'B':
		case 'b':	cout << "\nPlease enter how many you would like to purchase: ";
					cin >> quantity_sold;
					quantity_soldB += quantity_sold;
					totalB = quantity_soldB * b;
					break;
		case 'C':
		case 'c':	cout << "\nPlease enter how many you would like to purchase: ";
					cin >> quantity_sold;
					quantity_soldC += quantity_sold;
					totalC = quantity_soldC * c;
					break;
		case 'N':
		case 'n':			
					break;
		default: cout << "You must enter a valid product type! " << endl;
		}
	} while (product_type != 'N');


	total_sales = totalA + totalB + totalC;


	cout << "\nThe total sales for product A are $" << totalA << endl << endl;
	cout << "The total sales for product B are $" << totalB << endl << endl;
	cout << "The total sales for product C are $" << totalC << endl << endl;
	cout << "The total amount of all products before your discount is $" << total_sales << endl << endl;


	//cout << "Discount: ";
	discount = (floor(total_sales / 20) * 2);
		cout << "Your total discount is $" << discount << "." << endl;
	//cout << "Amount due: ";
	net = total_sales - discount;
		cout << "Your amount due after your discount is $" << net << "." << endl;
	cout << "Thank you for shopping at Mail Mart! Enjoy the rest of your day! " << endl << endl;
	
	return 0;
}

Last edited on
Thanks for the reply and help. The N part was driving me crazy. I will look into what you said about it later on. The math behind the discount I found the answer to earlier today.

Edit: You're right about the N part. It makes. Thanks. I will try to figure out a way to do it with the product type and quantity on the same line, though.

Edit: I just figured it out. Thanks for everything. Your info on the N part about how having my cin on the same line really helped.
Last edited on
Topic archived. No new replies allowed.