Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Redo Exercise 4 so that the customer can select multiple items of a particular t

ID: 3722095 • Letter: R

Question

Redo Exercise 4 so that the customer can select multiple items of a particular type. A sample output in this case is:

programming language is c++

code to redo is:

#include
#include
#include
#include

using namespace std;

const int NO_OF_ITEMS = 8;

struct menuItemType
{
string menuItem;
double menuPrice;
};

void getData(ifstream& inFile, menuItemType mList[], int listSize);
void showMenu(menuItemType mList[], int listSize);
void printCheck(menuItemType mList[], int listSize,
int cList[], int cListLength);
void makeSelection(int listSize,
int cList[], int& cListLength);
bool isItemSelected(int cList[], int cListLength, int itemNo);

int main()
{
menuItemType menuList[NO_OF_ITEMS];
int choiceList[NO_OF_ITEMS];
int choiceListLength;

ifstream inFile;

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

inFile.open("Ch9_Ex4Data.txt");

if (!inFile)
{
cout << "Cannot open the input file. Program Terminates!"
<< endl;
return 1;
}

getData(inFile, menuList, NO_OF_ITEMS);
showMenu(menuList, NO_OF_ITEMS);
makeSelection(NO_OF_ITEMS,
choiceList, choiceListLength);
printCheck(menuList, NO_OF_ITEMS,
choiceList, choiceListLength);

return 0;
}

void getData(ifstream& inFile, menuItemType mList[], int listSize)
{
char ch;
   for (int i = 0; i < listSize; i++)
{
getline(inFile, mList[i].menuItem);
inFile >> mList[i].menuPrice;
inFile.get(ch);
}
}

void showMenu(menuItemType mList[], int listSize)
{
cout << "Welcome to Johnny's Resturant" << endl;
cout << "----Today's Menu----" << endl;

for (int i = 0; i < listSize; i++)
cout << i+1 << ": " << left << setw(15) << mList[i].menuItem
<< right << " $" << mList[i].menuPrice << endl;
cout << endl;
}

void printCheck(menuItemType mList[], int listSize,
               int cList[], int cListLength)
{
int i;
double salesTax;
double amountDue = 0;

cout << "Welcome to Johnny's Resturant" << endl;
for (i = 0; i < cListLength; i++)
{
cout << left << setw(15) << mList[cList[i]].menuItem
<< right << " $" << setw(4) << mList[cList[i]].menuPrice << endl;
amountDue += mList[cList[i]].menuPrice;
}

salesTax = amountDue * .05;
cout << left << setw(15) << "Tax " << right << " $"
<< salesTax << endl;
amountDue = amountDue + salesTax;
cout << left << setw(15) << "Amount Due " << right
<< " $" << amountDue << endl;
}

void makeSelection(int listSize,
int cList[], int& cListLength)
{
int selectionNo = 0;
int itemNo;

char response;

cListLength = 0;

cout << "You can make up to " << listSize
<< " single order selections" << endl;

cout << "Do you want to make selection Y/y (Yes), N/n (No): ";
cin >> response;
cout << endl;

while ((response == 'Y' || response == 'y') &&
cListLength < 8)
{
cout << "Enter item number: ";
cin >> itemNo;
cout << endl;

if (!isItemSelected(cList,cListLength,itemNo))
cList[cListLength++] = itemNo - 1;
else
cout << "Item already selected" << endl;

cout << "Select another item Y/y (Yes), N/n (No): ";
cin >> response;
cout << endl;
}
}

bool isItemSelected(int cList[], int cListLength, int itemNo)
{
bool found = false;

for (int i = 0; i < cListLength; i++)
if (cList[i] == itemNo)
{
found = true;
break;
}

return found;
}

Explanation / Answer

Hi,

I have gone through the program logic and found that program is looping when select another item is Y/y in makeSelection() function and selection of items are written into choicelist array.

printCheck() function loops the length of choices and prints the amount with price.

in makeSelection(), below logic repeats untill response is Y/y or cListlength is less than 8

while ((response == 'Y' || response == 'y') && cListLength < 8)
{
cout << "Enter item number: ";
cin >> itemNo;
cout << endl;

if (!isItemSelected(cList,cListLength,itemNo))
cList[cListLength++] = itemNo - 1;
else
cout << "Item already selected" << endl;

cout << "Select another item Y/y (Yes), N/n (No): ";
cin >> response;
cout << endl;
}

printCheck() prints items with amount untill cListlength.

for (i = 0; i < cListLength; i++)
{
cout << left << setw(15) << mList[cList[i]].menuItem<< right << " $" << setw(4) << mList[cList[i]].menuPrice << endl;
amountDue += mList[cList[i]].menuPrice;
}

salesTax = amountDue * .05;
cout << left << setw(15) << "Tax " << right << " $"<< salesTax << endl;
amountDue = amountDue + salesTax;
cout << left << setw(15) << "Amount Due " << right<< " $" << amountDue << endl;
}

As program is invlolved files, I couldn't execute it in online compiler. Could you please give a shot and verify the required functionality

while ((response == 'Y' || response == 'y') && cListLength < 8)
{
cout << "Enter item number: ";
cin >> itemNo;
cout << endl;

if (!isItemSelected(cList,cListLength,itemNo))
cList[cListLength++] = itemNo - 1;
else
cout << "Item already selected" << endl;

cout << "Select another item Y/y (Yes), N/n (No): ";
cin >> response;
cout << endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote