Question: C++ Start with the checkout.cpp file provided. You must make a class that works with the existing main() function that simulates checking out at a

C++

Start with the checkout.cpp file provided.

You must make a class that works with the existing main() function that simulates checking out at a grocery store. You may

assume fewer than 200 items will be checked out. Ensure that you class makes the provided main() output match the

examples exactly. The add() function should add a checkout item to the items being bought, eitherincreasing the

amount by one if the item is already present or adding it to the list if it is new. You maydirectly compare strings when adding

, thus if the string is different at all it can be treated as a new item. The checkout() function should return a list of all items

and their quantities, along with a final additional line saying how many items were bought. Note that the item bought is the sum of the

quantities, not the amount of different types of items on the list. The order that you display the items isnot important

(but the order the examples use is probably the easiest to do).

[Hint: A easy way to get numbers into string is to use the overloaded += operator for strings along with

the int2str() function provided, for example:

string x = ;

x += int2str(2);

x += x Apples;

cout << x; // shows 2x Apples

]

Example 1 (user input is bold):

apple

banana

apple

!checkout

Receipt:

2x apple

1x banana

Total items: 3

Example 2 (user input is bold):

apple

banana

Apple

Checkout

!checkout

Receipt:

1x apple

1x banana

1x Apple

1x Checkout

Total items: 4

Example 3 (user input is bold):

pasta

milk

pasta

milk

rice

milk

rice

milk

!checkout

Receipt:

2x pasta

4x milk

2x rice

Total items: 8

Checkout.cpp:

#include #include #include #include

using namespace std;

string int2str(int x);

class GroceryList { // Make me! };

int main() { GroceryList gl; string item; getline(cin, item); while(item != "!checkout") { gl.add(item); getline(cin, item); } int size; string* receipt = gl.checkout(size); // this should initialize size cout << " Receipt: "; int maxLen = 0; for(int i=0; i < size-1; i++) // list of items, but not total (last in array) { cout << receipt[i] << endl; if(static_cast(receipt[i].length()) > maxLen) { maxLen = receipt[i].length(); } } for(int i=0; i < maxLen; i++) { cout << "-"; } cout << endl; cout << receipt[size-1] << endl; // total number of items delete [] receipt; return 0; }

string int2str(int x) { string reverse = ""; int pos = abs(x); while(pos > 0) { reverse += ('0'+pos%10); pos/=10; } if(reverse.length() == 0) { return "0"; } string result = ""; if(x < 0) { result += '-'; } for(int i=0; i < static_cast(reverse.length()); i++) { result += reverse[reverse.length()-1-i]; } return result; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To solve this problem you need to define the GroceryList class such that it contains methods to add items and check them out in a way that conforms to ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!