Question: 1. Parallel Arrays to Struct You are given a program that uses parallel arrays to read the product prices from a configuration file and does

1. Parallel Arrays to Struct

You are given a program that uses parallel arrays to read the product prices from a configuration file and does the checkout operations for a simple store. Review the whole program first, then convert it to use one array of struct, instead of parallel arrays. Do not change the functionality of the program. In other words, no changes to input/output/file format. Here is the copy of configuration file products.txt

18 4101 BRAEBURN_REG 0.99 4021 DELICIOUS_GDN_REG 0.89 4020 DELICIOUS_GLDN_LG 1.09 4015 DELICIOUS_RED_REG 1.19 4016 DELICIOUS_RED_LG 1.29 4167 DELICIOUS_RED_SM 0.89 4124 EMPIRE 1.14 4129 FUJI_REG 1.05 4131 FUJI_X-LGE 1.25 4135 GALA_LGE 1.35 4133 GALA_REG 1.45 4139 GRANNY_SMITH_REG 1.39 4017 GRANNY_SMITH_LGE 1.49 3115 PEACHES 2.09 4011 BANANAS 0.49 4383 MINNEOLAS 0.79 3144 TANGERINES 1.19 94011 ORGANIC_BANANAS 0.99 

GIVEN CODE:

#include

#include // input/output manipulation

#include

#include

using namespace std;

const int MAXPRODUCTS = 1000;

/*

int codes[MAXPRODUCTS];

string products[MAXPRODUCTS];

double prices[MAXPRODUCTS];

*/

struct Product {

int code;

string name;

double price;

}

Product products[MAXPRODUCTS];

int numProducts = 0;

//complete setup done here!

void setup()

{

ifstream finput("products.txt");

//get # of products first.

finput >> numProducts;

//get product codes, names & prices.

for(int i=0; i

finput >> codes[i] >> products[i] >> prices[i];

}

//list all the products available.

void listProducts()

{

//list all the available products.

cout << "Available products: ";

for(int i=0; i

cout << codes[i] << ": " << products[i] << " @ " << prices[i] << "/pound. ";

}

//return index of product with specified PLU

//return -1 if not found

int findIndex(int inputCode)

{

//look for inputCode in codes[] array

//return corresponding index!

for(int i=0; i

if (inputCode == codes[i])

return i;

return -1;

}

int getUserSelection()

{

int plu, selection = 0;

while (true)

{

listProducts();

cout << "Enter PLU (0 to end): ";

cin >> plu;

if (plu == 0)

return -1;

selection = findIndex(plu);

if (selection >= 0)

return selection;

cout << "Invalid selection. ";

}

}

//checkout functionality for one customer

//returns the total amount

double checkout()

{

double total = 0, weight = 0;

cout <

while (true)

{

int selection = getUserSelection();

if (selection < 0)

break;

cout << "Enter weight for " << products[selection] <<": ";

cin >> weight;

total += weight * prices[selection];

cout << "Total so far: $ " << total << endl;

}

cout << "Final total: $ " << total << endl;

return total;

}

int main()

{

double grandTotal = 0;

char response = 'n';

setup();

while (response == 'n')

{

grandTotal += checkout();

cout << "(n)ext customer OR (c)lose the register? ";

cin >> response;

}

cout << "Grand total: $" << grandTotal << endl;

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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!