Write a program that acts as a self-checkout register at a store and lets the user buy 2 items

The program will prompt the user and read in:
- the name of item 1. The name can be multiple words with space in between.
- the price of item 1 and number of items 1. Read both the price and the number in one C++ statement
- the name of item 2. The name can be multiple words with space in between (cin won’t work)
- the price of item 2 and the number of items 2. Read both the price and the number in one C++ statement

The program calculates the total cost of the items, including the sales tax of 9.25%.
The tax should be defined as a constant in the program, and not used as a literal.

Last, the program prints the receipt on screen in the following format:
Item            Count   Price   Ext. Price
====            =====   =====   ========
Item 1 Name       Num   Price   Price * Num
Item 2 Name       Num   Price   Price * Num
Tax                             Tax * SubTotal
Total                           Total
The output should be in column format, and the prices are with 2 digits after the decimal point.
You can assume the item names will not be more than 20 characters wide.

Example program output:
(In this example the user buys mechanical pencils and binders, note that the item name is repeated in the second prompt of each item)
Enter the name of item 1:  mechanical pencil
Enter the number of mechanical pencil(s) and the price of each:  2  2.50
Enter the name of item 2:  binder
Enter the number of binder(s) and the price of each:  3  7.25
 
Item               Count   Price   Ext. Price
====               =====   =====   ========
mechanical pencil      2    2.50       5.00
binder                 3    7.25      21.75
Tax                                    2.31
Total                                 29.06

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float price1, price2; // The price of 2 items
int quantity1, quantity2; // The quantity of 2 items


cout << setprecision(2) << fixed << showpoint;
cout << "Please input the price and quantity of the first item" << endl;

cin >> price1 >> quantity1;

cout << endl << "Please input the price and quantity of the second item\n";
cin >> price2 >> quantity2;

cout << left << setw(16) << "PRICE" << right << setw(12) << "QUANTITY" << endl << endl;

cout << setw(15) << price1 << setw(9) << quantity1 << endl;
cout << setw(15) << price2 << setw(9) << quantity2 << endl;

return 0;

}

Last edited on
What issues are you having for this assignment?
I'm new to c++ coding so IDk how to do this and how to start thx! It should be easy but i'm having a very hard time!
You should include a string, which holds the name of the items.
Something like, string itemName;

The name can be multiple words with space in between (cin won’t work)
It means you should use getline so, it would look like this getline(cin, itemName); It would allow you to type the item name with spaces.

sales tax of 9.25%.

You should define the a constant variable that holds the sales tax.

Below is to just give you an idea.

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
#include <iostream>
#include <string>
#include <iomanip>
#include <limits>

// a Cash Register struct to hold the following members.
struct CashRegister
{
	std::string item;
	double price;
	unsigned int numItems;
};

//Function prototypes.
void inputData(CashRegister[], const unsigned int);
void print(CashRegister[], const unsigned int, const double);

int main()
{
	const unsigned int numOfItems{2}; // Holds the size of the array.
	const double salesTax{.0925}; // 9.25% sales tax.
	CashRegister selfRegister[numOfItems];

	inputData(selfRegister, numOfItems); // Call the function to enter the data.
	print(selfRegister, numOfItems, salesTax); // Call the function to print.

	return 0;
}

/*
	This function is used for a for loop to allow the user
	to input the required information for the cash register.
*/
void inputData(CashRegister selfRegister[], const unsigned int numOfItems)
{
	for (std::size_t count = 0; count < numOfItems; count++)
	{
		std::cout << "Enter the name of item " << count + 1 << ": ";
		std::getline(std::cin, selfRegister[count].item);

		std::cout << "Enter the number of " << selfRegister[count].item
			<< "(s) and the price of each: ";
		std::cin >> selfRegister[count].numItems >> selfRegister[count].price;

		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}
}

void print(CashRegister selfRegister[], const unsigned int numOfItems, const double salesTax)
{
	double runningTotal{0.0};
	double extPrice{};
	double tax{};
	std::cout << "Item\t\t\tCount\tPrice\tExt.Price\n";
	std::cout << "====\t\t\t=====\t=====\t=========\n";

	for (std::size_t count = 0; count < numOfItems; count++)
	{
		std::cout << std::left << std::setw(20) << selfRegister[count].item << "\t";
		std::cout << selfRegister[count].numItems << "\t";
		std::cout << std::fixed << std::setprecision(2) 
			<< selfRegister[count].price << "\t";
		extPrice = selfRegister[count].numItems * selfRegister[count].price;
		runningTotal += extPrice;
		std::cout << extPrice << std::endl;
		std::cout << std::fixed << std::setprecision(0);
	}
	tax = runningTotal * salesTax;
	std::cout << std::fixed << std::setprecision(2);
	std::cout << "Tax\t\t\t\t\t" << tax << std::endl;
	std::cout << "Total\t\t\t\t\t" << runningTotal + tax << std::endl;
}
Last edited on
is that how the code format should look like?!
Not at all, the code is just to give you an idea. My recommendation would be to break down the program into small parts. Read about getline from the string library. You will probably have to use the cin.ignore() so I would read up on it.

The same recommendation that you got in your other post still applies (i.e. setw and setprecision())

It looks like you are on a right track. Keep working on your code and keep posting the updates as you go.
This should be a easy code right?!
Yes, if you applied what you learn in class.
Topic archived. No new replies allowed.