Question

Need help with C++ assignment

Assignment 1 and .txt files are provided at the bottom.

PART A

PART B

Assignment 1

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <algorithm>

using namespace std;

/**

This structure is to store the date and it has three integer fields

**/

struct Date{

   int day;

   int month;

   int year;

};

/**

This structure is to store the size of the box and it also have three int fields

**/

struct SizeOfBox{

int length;

int   width;

int height;

};

/**

This is the main shipment structure which contains the instances of the above structures

as member of structure. You can see that we are using the same Date struct for both expiry date

and shipment received date. We are just creating different instance of the struct

**/

struct Shipment {

string foodName;

Date expdate;

SizeOfBox sizeofbox;

float weightOfBox;

char storageMethod;

Date shipmentrecieved;

double priceofitem;

};

/**

This is the struct for storing customer information

**/

struct Customers{

string customerName;

string city;

int distance;

};

/**

This is the struct used to store the information about orders. this struct also contains the

instances of other structures

**/

struct Orders{

string customerName;

string itemName;

int numberOfBoxes;

double costofitem;

Date orderDate;

};

/**

As we are also using shipment struct for searching we are using a fixed number for shipment

which is 20. This is the maximum size the shipment array can be

**/

const int NO_OF_SHIPMENTS = 20;

/**

This is the object name for the struct Shipment of size 20

**/

Shipment shipments[NO_OF_SHIPMENTS];

/**

The following are the functions that are used for opening the data file and reading

contents into the respective struct arrays. Each function takes the input stream

as the parameter.

These three functions are mandatory as instructed in the Assignment

**/

void getDataShipments(ifstream& inFile);

void getDataOrders(ifstream& inFile);

void getDataCustomers(ifstream& inFile);

/**

The following are the functions that are used for displaying the

contents from the respective struct arrays on Monitor.

Each function takes the respective struct array and total items.

**/

void displayShipments(Shipment shipments[],int totalShipments);

void displayCustomers(Customers customer[], int totalCustomers);

void displayOrders(Orders orders[], int totalOrders);

/**

These functions are used for printing horizontal line and heading before you display

the contents of the struct

**/

void printHorizontalLine( int width, char border_char);

void printHeading( string title, int width );

/**

This function is used to search the shipment array by taking the shipment struct array

the item name to be searched and total number of shipments as the parameters

**/

void searchShipments(Shipment shipments[], string itemName, int totalShipments);

/**

This is the main function and it is mandatory that it should contains only declarations and function calls

**/

int main(){

cout << "This is Assignment 1 dealing with Structures" << endl;

/**

Declaring the input streams for each file

**/

ifstream inDataShipments;

ifstream inDataCustomers;

ifstream inDataOrders;

/**

Opening the files. You can either hardcode the name of the files or ask the user to give the names

**/

inDataShipments.open("Shipments.txt");

inDataCustomers.open("Customers.txt");

inDataOrders.open("Orders.txt");

/**

If the any of the file cannot be opened then the program terminates displaying

the error message

**/

if (!inDataShipments)

{

cout << "The Shipments input file does not exist. Program terminates!" << endl;

return 1;

}

if (!inDataCustomers)

{

cout << "The Customers input file does not exist. Program terminates!" << endl;

return 1;

}

if (!inDataOrders)

{

cout << "The Orders input file does not exist. Program terminates!" << endl;

return 1;

}

/**

Function calls to read data from input files.

**/

/**

   One way to display the information is to automatically display the information after reading from the input

   file or give the user for option to which information user wants to print

   In this we have called the Display******* functions after reading the data. And finally asked the user weather

   user wants to search or not.

**/

getDataShipments(inDataShipments);

getDataCustomers(inDataCustomers);

getDataOrders(inDataOrders);

/**

After displaying the contents that are read from the file, program is asking weather the user want to search or not

**/

int searchOption;

do{

cout << "\n\n\nDo You Want to Search in Shipments \n"

   << "1. Yes \n"

   << "2. No \n"

   << "\n"

   << "Enter Option (1-2): ";

cin >> searchOption;

/**

If the user wants to search, it asks for the name of the item and calls the appropriate function to search

else the program terminates

**/

if(searchOption == 1){

string itemName;

cout << "Enter the Item to Search:" << endl;

cin >> itemName;

searchShipments(shipments,itemName,NO_OF_SHIPMENTS);

}

}while(searchOption != 2);

if(searchOption == 2){

exit(0);

}

}

void printHorizontalLine( int width, char border_char){

cout.fill( border_char );

cout << setw( width ) << border_char << "\n";

cout.fill(' ');

}

void printHeading( string title, int width ){

//Declare Variables

int magic_width = 0;

magic_width = (width/2) - (title.length()/2) + title.length();

cout << "\n";

cout << left << setfill('=') << setw( width ) << "=" << "\n";

cout << right << setfill(' ') << setw( magic_width ) << title << "\n";

cout << right << setfill('=') << setw( width ) << "=" << endl;

//reset count

cout.clear();

cout.fill(' ');

//VOID functions do NOT return a value

}

/**

Using the input stream sent as parameter we are reading the content from the Shipments.txt and storing it in the shipments struct array

**/

void getDataShipments(ifstream& inFile){

char decimal;

int totalShipmentItems;

inFile >> totalShipmentItems;

for(int i = 0; i < totalShipmentItems; i++){

inFile >> shipments[i].foodName;

inFile >> shipments[i].expdate.month >> decimal >> shipments[i].expdate.day >> decimal >> shipments[i].expdate.year;

inFile >> shipments[i].sizeofbox.length >> decimal >> shipments[i].sizeofbox.width >> decimal >> shipments[i].sizeofbox.height;

inFile >> shipments[i].weightOfBox;

inFile >> shipments[i].storageMethod;

inFile >> shipments[i].shipmentrecieved.month >> decimal >> shipments[i].shipmentrecieved.day >> decimal >> shipments[i].shipmentrecieved.year;

inFile >> shipments[i].priceofitem;

}

inFile.close();

printHeading("Shipments", 80);

displayShipments(shipments,totalShipmentItems);

}

/**

Using the input stream sent as parameter we are reading the content from the Customers.txt and storing it in the customers struct array

**/

void getDataCustomers(ifstream& inFile){

int totalCustomerItems;

inFile >> totalCustomerItems;

Customers customers[totalCustomerItems];

for(int i = 0; i < totalCustomerItems; i++){

inFile >> customers[i].customerName;

inFile >> customers[i].city;

inFile >> customers[i].distance;

}

inFile.close();

printHeading("Customers", 60);

displayCustomers(customers,totalCustomerItems);

}

/**

Using the input stream sent as parameter we are reading the content from the Orders.txt and storing it in the orders struct array

**/

void getDataOrders(ifstream& inFile){

char decimal;

int totalOrderItems;

inFile >> totalOrderItems;

Orders orders[totalOrderItems];

for(int i = 0; i < totalOrderItems; i++){

inFile >> orders[i].customerName;

inFile >> orders[i].itemName;

inFile >> orders[i].numberOfBoxes;

inFile >> orders[i].costofitem;

inFile >> orders[i].orderDate.month >> decimal >> orders[i].orderDate.day >> decimal >> orders[i].orderDate.year;

}

inFile.close();

printHeading("Orders", 60);

displayOrders(orders, totalOrderItems);

}

/**

Displaying the content from the shipments struct array on the monitor

**/

void displayShipments(Shipment shipments[], int totalShipments){

   cout << setw(10) << "Food Item" << " | "

<< "Exp Date " << " | "

<< "Box Size" << " | "

<< "Weight" << " | "

<< "Storage" << " | "

<< "Received date" << " | "

<< "Price" << endl;

printHorizontalLine(80,'-');

for(int i = 0; i < totalShipments; i++){

cout << left << setw(9) << shipments[i].foodName << " | "

   << right <

   << right <

   << right <

   << right <

   << right <

   << right << fixed << setprecision(2) << setfill('0') << shipments[i].priceofitem << " "

   << setfill(' ') << endl;

   cout.unsetf(ios_base::fixed);

}

}

/**

Displaying the content from the customers struct array on the monitor

**/

void displayCustomers(Customers customers[], int totalCustomers){

cout << "Customer Name" << " | "

   << "City" << " | "

   << "Distance" << endl;

printHorizontalLine(60,'-');

for(int i = 0; i < totalCustomers; i++){

cout << left << setw(10) << customers[i].customerName << " | "

   << left << setw(10) << customers[i].city << " | "

   << right << setw(2) << setfill('0') << customers[i].distance << " "

   << setfill(' ') << endl;

}

}

/**

Displaying the content from the orders struct array on the monitor

**/

void displayOrders(Orders orders[],int totalOrders){

cout << setw(10) << setfill(' ') << "Customer Name" << " | "

   << "Item Name" << " | "

   << "#Boxes" << " | "

   << "Cost" << " | "

   << "Order Date" << endl;

printHorizontalLine(60,'-');

for(int i = 0; i < totalOrders; i++){

cout << left << setw(10) << orders[i].customerName << " | "

   << left << setw(10) << orders[i].itemName << " | "

   << right << setw(2) << setfill('0') << orders[i].numberOfBoxes << " | "

   << right << setw(4) << setfill('0') << orders[i].costofitem << " | "

   << right << setw(2) << setfill('0') << orders[i].orderDate.month <<":" << setw(2) << setfill('0') << orders[i].orderDate.day <<":" << setw(2) << setfill('0') << orders[i].orderDate.year << " "

   << setfill(' ') << endl;

}

}

/**

Function used to search the shipments struct array and if there is a match then

it displays all the information about the product on the monitor

**/

void searchShipments(Shipment shipment[], string itemName, int totalShipments){

string originalItemName;

bool searchHit = false;

   cout << "\n\nSearching for Food Item: " << itemName << "\n\n"<< endl;

/**

   Converts the user input value of item name to Upper Case, so that we can have case insensitive copmarision

   **/

transform(itemName.begin(),itemName.end(),itemName.begin(), ::toupper);

printHeading("Search Results",80);

cout << setw(10) << "Food Item" << " | "

   << "Exp Date " << " | "

   << "Box Size" << " | "

   << "Weight" << " | "

   << "Storage" << " | "

   << "Received date" << " | "

   << "Price" << endl;

printHorizontalLine(80,'-');

for(int i = 0; i < totalShipments; i++){

   /**

       Converts the item name to Upper Case without modifying the original data, so that we can have case insensitive comparision

       **/

       originalItemName = shipment[i].foodName;

transform(originalItemName.begin(),originalItemName.end(),originalItemName.begin(),::toupper);

if(itemName.compare(originalItemName) == 0){

searchHit = true;

cout << left << setw(9) << shipments[i].foodName << " | "

   << right <

   << right <

   << right <

   << right <

   << right <

   << right << fixed << setprecision(2) << setfill('0') << shipments[i].priceofitem << " "

   << setfill(' ') << endl;

cout.unsetf(ios_base::fixed);

}

}

if(!searchHit){

cout << "No Items Found in Shipments! Try Again" << endl;

}

}

Shipments.txt

100

Tomato 09:20:2018 01:30:32 18.11 D 04:25:2015 44.00

EggPlant 01:30:2013 02:28:34 55.55 R 01:26:2015 20.00

EggPlant 09:10:2017 04:26:36 55 D 02:28:2015 16.69

Bananas 09:15:2013 24:38:60 40.66 F 01:26:2015 39.93

Potato 09:22:2015 08:22:40 20.2 D 01:29:2015 39.93

EggPlant 04:10:2019 10:20:42 76.18 R 10:11:2016 11.00

Grapes 06:30:2016 12:18:44 40.4 F 02:25:2015 20.00

Bread 07:10:2016 14:16:46 55 F 09:18:2015 24.25

Lays 09:10:2015 03:42:45 55 D 11:16:2015 39.93

Bananas 06:13:2015 06:39:48 80.8 R 01:29:2015 39.93

Potato 01:25:2015 09:36:51 24.42 D 01:30:2015 64.46

Apples 02:26:2019 12:33:54 58.85 R 03:13:2015 16.69

Lays 03:10:2015 15:30:57 70.7 D 01:29:2015 20.00

Plum 09:20:2017 18:27:60 96.69 D 12:12:2015 16.69

Plum 07:10:2018 21:24:63 55 F 02:18:2015 64.46

Lays 09:12:2020 04:64:68 12 F 07:22:2015 20.00

Potato 09:12:2016 08:60:72 28 R 07:22:2015 39.93

PineApple 06:28:2016 12:56:76 35.53 F 10:25:2015 39.93

Apples 08:10:2016 16:52:80 55 F 09:12:2015 64.46

Potato 06:18:2018 20:48:84 45.54 R 10:25:2015 16.69

Chicken 08:13:2018 24:44:88 99.99 R 10:11:2016 80.00

Lays 08:16:2018 28:40:92 25.12 D 04:25:2015 12.00

EggPlant 07:10:2015 32:36:96 55 R 01:12:2015 20.00

Tomato 09:10:2016 94:76:74 55 R 07:29:2015 39.93

Apples 07:10:2018 92:78:72 55 F 03:26:2015 20.00

EggPlant 08:27:2017 90:80:70 50.5 D 02:25:2015 24.25

Lays 01:10:2015 88:82:68 50 D 08:26:2015 39.93

EggPlant 07:13:2016 86:84:66 26.62 F 12:12:2015 39.93

Apples 02:30:2017 62:20:16 20.12 R 04:25:2015 12.00

Chicken 06:25:2015 58:24:14 30 R 01:29:2015 20.00

Bananas 01:29:2016 54:12:06 34.43 R 01:30:2015 39.93

EggPlant 01:28:2015 50:32:10 34.43 D 01:30:2015 20.00

Bread 06:14:2016 46:36:08 55.55 F 10:25:2015 39.93

Lays 02:29:2018 42:38:06 88.88 F 03:13:2015 20.00

Lays 09:10:2015 01:18:19 55 D 03:22:2015 20.00

Plum 02:23:2015 02:17:20 28.82 R 03:13:2015 20.00

Grapes 05:26:2017 03:16:21 25.52 D 10:25:2015 64.46

Pears 02:25:2015 04:15:22 48.84 D 03:13:2015 24.25

Grapes 08:19:2017 05:14:23 86.68 R 12:12:2015 39.93

Tomato 09:12:2014 06:13:24 56 F 07:22:2015 20.00

Lays 08:11:2019 07:12:25 65.56 D 10:25:2015 24.25

Grapes 02:24:2016 08:11:26 58.85 F 03:13:2015 39.93

Potato 09:18:2019 09:10:27 76.67 D 12:12:2015 20.00

Chicken 09:12:2019 28:39:40 80 F 07:22:2015 20.00

PineApple 07:17:2017 29:38:41 66.66 F 12:12:2015 20.00

Bananas 08:10:2016 30:37:42 55 F 01:29:2015 24.25

Apples 08:10:2016 31:36:43 55 F 01:28:2015 24.25

Grapes 03:28:2015 33:34:45 40 D 01:29:2015 64.46

Tomato 01:26:2016 46:55:56 34.43 F 01:30:2015 39.93

Potato 05:10:2016 47:54:57 55.44 F 08:26:2015 39.93

Chicken 07:10:2015 48:53:58 55 D 08:21:2015 20.00

Bananas 02:10:2015 49:52:59 86 R 08:26:2015 24.25

Chicken 04:22:2015 50:51:60 55 F 01:26:2015 80.52

Pears 09:16:2015 61:74:75 90.9 D 01:29:2015 20.00

Pears 09:10:2019 62:73:76 55 F 06:12:2015 24.25

Bananas 09:10:2019 63:72:77 55 D 11:14:2015 16.69

PineApple 01:24:2016 64:71:78 14.41 R 01:30:2015 16.69

Tomato 08:22:2016 65:70:79 60.6 R 02:25:2015 20.00

Bananas 07:15:2019 66:69:80 46.64 R 12:12:2015 39.93

Lays 08:25:2016 67:68:81 15.51 R 10:25:2015 20.00

Grapes 09:13:2018 82:93:94 10 R 07:22:2015 39.93

Carrot 02:15:2018 83:92:96 55.55 F 10:11:2016 32.00

Apples 09:12:2015 05:04:06 57 D 07:22:2015 24.25

Potato 08:10:2019 84:91:97 55 D 04:26:2015 64.46

Chicken 07:16:2016 86:89:99 56.65 F 12:12:2015 24.25

PineApple 09:10:2020 87:88:98 55 R 05:24:2015 39.93

Bread 08:16:2019 97:82:81 55.55 D 10:11:2016 24.25

PineApple 09:12:2018 96:83:82 78 R 07:22:2015 39.93

Oranges 06:11:2018 95:84:83 55.55 D 10:11:2016 16.00

Grapes 08:22:2017 94:85:84 55 D 04:25:2015 64.00

Grapes 08:10:2018 93:86:82 55 D 05:10:2015 20.00

PineApple 04:25:2013 92:87:88 65.56 D 01:26:2015 39.93

Apples 07:12:2019 91:88:86 16.61 F 12:12:2015 16.69

Grapes 08:10:2017 90:89:87 55 D 08:20:2015 20.00

Bananas 02:28:2019 05:04:06 78.87 D 03:13:2015 64.46

Bread 09:10:2016 05:04:06 55 R 04:16:2015 39.93

Tomato 09:10:2018 05:04:06 55 R 07:22:2015 39.93

PineApple 04:22:2018 05:04:06 40 R 04:25:2015 55.00

Tomato 07:14:2017 05:04:06 36.63 R 12:12:2015 20.00

Oranges 07:22:2015 05:04:06 55 F 01:10:2015 20.00

Chicken 09:10:2018 05:04:06 55 R 12:14:2015 20.00

Potato 08:18:2017 05:04:06 66.12 D 04:25:2015 34.00

Plum 08:08:2013 05:04:06 55 D 01:26:2015 20.00

EggPlant 02:27:2018 05:04:06 68.86 R 03:13:2015 39.93

Apples 08:10:2016 05:04:06 28.72 F 08:26:2015 64.46

Plum 08:10:2018 05:04:06 55 D 01:20:2015 16.69

Pears 08:20:2013 05:04:06 10 F 01:26:2015 24.25

PineApple 02:30:2019 05:04:06 98.89 D 03:13:2015 39.93

PineApple 04:21:2017 05:04:06 30.3 D 02:25:2015 39.93

Apples 09:19:2015 05:04:06 10.1 F 01:29:2015 24.25

Bread 02:20:2017 05:04:06 20.2 F 02:25:2015 20.00

Pears 09:10:2015 05:04:06 55 F 01:27:2015 20.00

Pears 09:14:2019 05:04:06 55.55 D 10:11:2016 21.00

Bananas 04:28:2018 05:04:06 25.25 R 04:25:2015 15.00

Pears 09:10:2019 05:04:06 55 D 06:22:2015 16.69

Tomato 07:10:2015 05:04:06 17.71 D 08:26:2015 16.69

Grapes 09:12:2017 05:04:06 68 F 07:22:2015 20.00

Kiwi 08:12:2019 05:04:06 88.88 F 10:11:2016 14.00

Chicken 07:10:2015 05:04:06 55 R 01:26:2015 20.00

Apples 06:26:2017 05:04:06 20.12 D 04:25:2015 40.00

Orders.txt

100

Charlie Lays 07 32.00 01:21:2015

Jay Potato 05 12.32 03:14:2015

Pepp PineApple 03 37.54 01:13:2015

Rishi Tomato 03 34.43 12:13:2014

Jay Tomato 09 16.00 01:13:2015

Rishi Kiwi 10 88.99 12:13:2014

Sajan Oranges 20 87.00 10:12:2014

John EggPlant 05 24.20 03:16:2015

Rishi Grapes 05 38.80 01:12:2015

Vijay Grapes 20 14.30 01:13:2015

Pepp Chicken 18 14.30 12:13:2014

Jay Carrot 08 90.09 10:12:2014

Rishi PineApple 13 54.78 02:12:2015

Rishi Bananas 15 29.99 02:14:2015

Vijay Kiwi 10 88.98 12:25:2014

John Oranges 13 34.00 01:21:2015

Sajan Potato 15 99.90 12:13:2014

Charlie Apples 20 99.99 02:14:2015

Vijay PineApple 07 22.10 01:25:2015

Sajan Lays 03 18.26 03:10:2015

Sajan Potato 07 62.68 04:18:2015

John Apples 15 45.90 01:25:2015

Pepp Grapes 09 80.72 04:30:2015

Sajan Oranges 9 87.00 01:25:2015

Pepp Grapes 09 36.00 01:21:2015

Pepp Chicken 07 22.22 03:14:2015

Pepp EggPlant 03 15.52 02:12:2015

Pepp Bananas 05 10.10 10:12:2014

Smith Apples 02 80.00 10:12:2014

Rishi Potato 18 38.00 01:21:2015

John PineApple 07 16.28 03:18:2015

Smith PineApple 07 50.20 10:12:2014

Jay Grapes 07 78.54 02:12:2015

Rishi Apples 15 25.52 02:12:2015

John Oranges 09 42.24 01:12:2015

Rishi Apples 10 34.10 03:25:2015

Rishi Apples 09 20.24 03:12:2015

John Grapes 09 78.78 12:13:2014

Charlie EggPlant 15 08.99 01:13:2015

Anne Chicken 09 72.80 04:22:2015

Pepp Chicken 13 94.92 12:25:2014

Pepp Potato 09 65.50 01:25:2015

Pepp Chicken 15 99.22 01:12:2015

Anne PineApple 09 24.24 01:12:2015

Jay Tomato 13 54.00 01:12:2015

John Apples 10 28.00 01:21:2015

Andrew Apples 03 23.00 01:25:2015

Pepp Apples 10 29.92 01:12:2015

Charlie Grapes 10 11.11 02:12:2015

Finn PineApple 08 55.55 10:12:2014

Andrew Apples 11 89.55 01:12:2015

Pepp Oranges 13 14.30 03:16:2015

Rishi Lays 05 96.90 12:25:2014

Pepp EggPlant 10 30.30 02:14:2015

Jay Tomato 13 27.00 02:12:2015

Anne Apples 09 98.88 12:25:2014

Charlie Apples 07 20.85 10:12:2014

John Lays 03 26.18 03:18:2015

Charlie PineApple 03 77.89 01:13:2015

Pepp Potato 10 48.45 02:12:2015

Pepp Lays 05 08.99 01:25:2015

Sajan Potato 15 77.00 01:12:2015

Charlie Apples 18 66.64 04:14:2015

Vijay PineApple 15 30.00 01:21:2015

John Tomato 03 21.20 02:14:2015

Rishi EggPlant 09 19.03 01:25:2015

Jay PineApple 03 31.96 01:13:2015

Rishi Tomato 03 45.50 01:13:2015

Sajan PineApple 07 25.45 12:13:2014

Vijay Potato 09 41.50 02:14:2015

John Plum 09 55.98 01:12:2015

Pepp Bananas 09 22.00 01:21:2015

John Kiwi 05 20.20 02:12:2015

Anne Potato 09 30.14 03:22:2015

Sajan Tomato 10 10.34 03:22:2015

Rishi Grapes 13 60.70 04:20:2015

Sajan Oranges 13 76.76 04:26:2015

John Apples 20 78.74 04:28:2015

Rishi Potato 02 29.99 10:12:2014

Anne Oranges 15 18.00 01:13:2015

Pepp Lays 20 68.62 04:12:2015

Rishi Potato 18 90.96 12:25:2014

Rishi Bananas 15 70.60 04:10:2015

Vijay PineApple 09 17.13 02:12:2015

Charlie Grapes 13 26.60 01:25:2015

Pepp Grapes 18 32.12 03:24:2015

Anne Tomato 10 77.90 01:25:2015

Rishi Chicken 09 90.90 01:25:2015

John Oranges 03 69.69 01:13:2015

Rishi Lays 13 89.99 02:14:2015

Jay EggPlant 07 74.78 04:24:2015

Rishi Lays 20 10.10 02:12:2015

Rishi EggPlant 05 25.00 01:21:2015

Gracy Grapes 09 47.74 10:12:2014

Jay Bananas 07 28.16 03:20:2015

Charlie Tomato 07 92.94 12:25:2014

Sajan Tomato 15 22.55 01:13:2015

John Kiwi 05 64.66 04:16:2015

Ben PineApple 10 55.99 01:12:2015

Bruce Bananas 05 32.20 10:12:2014

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code has changed

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <algorithm>
using namespace std;
/**
This structure is to store the date and it has three integer fields
**/
struct Date{
int day;
int month;
int year;
};
/**
This structure is to store the size of the box and it also have three int fields
**/
struct SizeOfBox{
int length;
int width;
int height;
};
/**
This is the main shipment structure which contains the instances of the above structures
as member of structure. You can see that we are using the same Date struct for both expiry date
and shipment received date. We are just creating different instance of the struct
**/
struct Shipment {
string foodName;
Date expdate;
SizeOfBox sizeofbox;
float weightOfBox;
char storageMethod;
Date shipmentrecieved;
double priceofitem;
};
/**
This is the struct for storing customer information
**/
struct Customers{
string customerName;
string city;
int distance;
};
/**
This is the struct used to store the information about orders. this struct also contains the
instances of other structures
**/
struct Orders{
string customerName;
string itemName;
int numberOfBoxes;
double costofitem;
Date orderDate;
};
/**
As we are also using shipment struct for searching we are using a fixed number for shipment
which is 20. This is the maximum size the shipment array can be
**/
const int NO_OF_SHIPMENTS = 20;
/**
This is the object name for the struct Shipment of size 20
**/
Shipment shipments[NO_OF_SHIPMENTS];
/**
The following are the functions that are used for opening the data file and reading
contents into the respective struct arrays. Each function takes the input stream
as the parameter.
These three functions are mandatory as instructed in the Assignment
**/
void getDataShipments(ifstream& inFile);
void getDataOrders(ifstream& inFile);
void getDataCustomers(ifstream& inFile);
/**
The following are the functions that are used for displaying the
contents from the respective struct arrays on Monitor.
Each function takes the respective struct array and total items.
**/
void displayShipments(Shipment shipments[],int totalShipments);
void displayCustomers(Customers customer[], int totalCustomers);
void displayOrders(Orders orders[], int totalOrders);
/**
These functions are used for printing horizontal line and heading before you display
the contents of the struct
**/
void printHorizontalLine( int width, char border_char);
void printHeading( string title, int width );
/**
This function is used to search the shipment array by taking the shipment struct array
the item name to be searched and total number of shipments as the parameters
**/
void searchShipments(Shipment shipments[], string itemName, int totalShipments);
/**
This is the main function and it is mandatory that it should contains only declarations and function calls
**/
int main(){
cout << "This is Assignment 1 dealing with Structures" << endl;
/**
Declaring the input streams for each file
**/
ifstream inDataShipments;
ifstream inDataCustomers;
ifstream inDataOrders;
/**
Opening the files. You can either hardcode the name of the files or ask the user to give the names
**/
inDataShipments.open("Shipments.txt");
inDataCustomers.open("Customers.txt");
inDataOrders.open("Orders.txt");
/**
If the any of the file cannot be opened then the program terminates displaying
the error message
**/
if (!inDataShipments)
{
cout << "The Shipments input file does not exist. Program terminates!" << endl;
return 1;
}
if (!inDataCustomers)
{
cout << "The Customers input file does not exist. Program terminates!" << endl;
return 1;
}
if (!inDataOrders)
{
cout << "The Orders input file does not exist. Program terminates!" << endl;
return 1;
}
/**
Function calls to read data from input files.
**/
/**
One way to display the information is to automatically display the information after reading from the input
file or give the user for option to which information user wants to print
In this we have called the Display******* functions after reading the data. And finally asked the user weather
user wants to search or not.
**/
getDataShipments(inDataShipments);
getDataCustomers(inDataCustomers);
getDataOrders(inDataOrders);
/**
After displaying the contents that are read from the file, program is asking weather the user want to search or not
**/
int searchOption;
do{
cout << "\n\n\nDo You Want to Search in Shipments \n"
<< "1. Yes \n"
<< "2. No \n"
<< "\n"
<< "Enter Option (1-2): ";
cin >> searchOption;
/**
If the user wants to search, it asks for the name of the item and calls the appropriate function to search
else the program terminates
**/
if(searchOption == 1){
string itemName;
cout << "Enter the Item to Search:" << endl;
cin >> itemName;
searchShipments(shipments,itemName,NO_OF_SHIPMENTS);
}
}while(searchOption != 2);
if(searchOption == 2){
exit(0);
}
}
void printHorizontalLine( int width, char border_char){
cout.fill( border_char );
cout << setw( width ) << border_char << "\n";
cout.fill(' ');
}
void printHeading( string title, int width ){
//Declare Variables
int magic_width = 0;
magic_width = (width/2) - (title.length()/2) + title.length();
cout << "\n";
cout << left << setfill('=') << setw( width ) << "=" << "\n";
cout << right << setfill(' ') << setw( magic_width ) << title << "\n";
cout << right << setfill('=') << setw( width ) << "=" << endl;
//reset count
cout.clear();
cout.fill(' ');
//VOID functions do NOT return a value
}
/**
Using the input stream sent as parameter we are reading the content from the Shipments.txt and storing it in the shipments struct array
**/
void getDataShipments(ifstream& inFile){
char decimal;
int totalShipmentItems;
inFile >> totalShipmentItems;
for(int i = 0; i < totalShipmentItems; i++){
inFile >> shipments[i].foodName;
inFile >> shipments[i].expdate.month >> decimal >> shipments[i].expdate.day >> decimal >> shipments[i].expdate.year;
inFile >> shipments[i].sizeofbox.length >> decimal >> shipments[i].sizeofbox.width >> decimal >> shipments[i].sizeofbox.height;
inFile >> shipments[i].weightOfBox;
inFile >> shipments[i].storageMethod;
inFile >> shipments[i].shipmentrecieved.month >> decimal >> shipments[i].shipmentrecieved.day >> decimal >> shipments[i].shipmentrecieved.year;
inFile >> shipments[i].priceofitem;
}
inFile.close();
printHeading("Shipments", 80);
displayShipments(shipments,totalShipmentItems);
}
/**
Using the input stream sent as parameter we are reading the content from the Customers.txt and storing it in the customers struct array
**/
void getDataCustomers(ifstream& inFile){
int totalCustomerItems;
inFile >> totalCustomerItems;
Customers customers[totalCustomerItems];
for(int i = 0; i < totalCustomerItems; i++){
inFile >> customers[i].customerName;
inFile >> customers[i].city;
inFile >> customers[i].distance;
}
inFile.close();
printHeading("Customers", 60);
displayCustomers(customers,totalCustomerItems);
}
/**
Using the input stream sent as parameter we are reading the content from the Orders.txt and storing it in the orders struct array
**/
void getDataOrders(ifstream& inFile){
char decimal;
int totalOrderItems;
inFile >> totalOrderItems;
Orders orders[totalOrderItems];
for(int i = 0; i < totalOrderItems; i++){
inFile >> orders[i].customerName;
inFile >> orders[i].itemName;
inFile >> orders[i].numberOfBoxes;
inFile >> orders[i].costofitem;
inFile >> orders[i].orderDate.month >> decimal >> orders[i].orderDate.day >> decimal >> orders[i].orderDate.year;
}
inFile.close();
printHeading("Orders", 60);
displayOrders(orders, totalOrderItems);
}
/**
Displaying the content from the shipments struct array on the monitor
**/
void displayShipments(Shipment shipments[], int totalShipments){
cout << setw(10) << "Food Item" << " | "
<< "Exp Date " << " | "
<< "Box Size" << " | "
<< "Weight" << " | "
<< "Storage" << " | "
<< "Received date" << " | "
<< "Price" << endl;
printHorizontalLine(80,'-');
for(int i = 0; i < totalShipments; i++){
cout << left << setw(9) << shipments[i].foodName << " | "
<< right <
<< right <
<< right <
<< right <
<< right <
<< right << fixed << setprecision(2) << setfill('0') << shipments[i].priceofitem << " "
<< setfill(' ') << endl;
cout.unsetf(ios_base::fixed);
}
}
/**
Displaying the content from the customers struct array on the monitor
**/
void displayCustomers(Customers customers[], int totalCustomers){
cout << "Customer Name" << " | "
<< "City" << " | "
<< "Distance" << endl;
printHorizontalLine(60,'-');
for(int i = 0; i < totalCustomers; i++){
cout << left << setw(10) << customers[i].customerName << " | "
<< left << setw(10) << customers[i].city << " | "
<< right << setw(2) << setfill('0') << customers[i].distance << " "
<< setfill(' ') << endl;
}
}
/**
Displaying the content from the orders struct array on the monitor
**/
void displayOrders(Orders orders[],int totalOrders){
cout << setw(10) << setfill(' ') << "Customer Name" << " | "
<< "Item Name" << " | "
<< "#Boxes" << " | "
<< "Cost" << " | "
<< "Order Date" << endl;
printHorizontalLine(60,'-');
for(int i = 0; i < totalOrders; i++){
cout << left << setw(10) << orders[i].customerName << " | "
<< left << setw(10) << orders[i].itemName << " | "
<< right << setw(2) << setfill('0') << orders[i].numberOfBoxes << " | "
<< right << setw(4) << setfill('0') << orders[i].costofitem << " | "
<< right << setw(2) << setfill('0') << orders[i].orderDate.month <<":" << setw(2) << setfill('0') << orders[i].orderDate.day <<":" << setw(2) << setfill('0') << orders[i].orderDate.year << " "
<< setfill(' ') << endl;
}
}
/**
Function used to search the shipments struct array and if there is a match then
it displays all the information about the product on the monitor
**/
void searchShipments(Shipment shipment[], string itemName, int totalShipments){
string originalItemName;
bool searchHit = false;
cout << "\n\nSearching for Food Item: " << itemName << "\n\n"<< endl;
/**
Converts the user input value of item name to Upper Case, so that we can have case insensitive copmarision
**/
transform(itemName.begin(),itemName.end(),itemName.begin(), ::toupper);
printHeading("Search Results",80);
cout << setw(10) << "Food Item" << " | "
<< "Exp Date " << " | "
<< "Box Size" << " | "
<< "Weight" << " | "
<< "Storage" << " | "
<< "Received date" << " | "
<< "Price" << endl;
printHorizontalLine(80,'-');
for(int i = 0; i < totalShipments; i++){
/**
Converts the item name to Upper Case without modifying the original data, so that we can have case insensitive comparision
**/
originalItemName = shipment[i].foodName;
transform(originalItemName.begin(),originalItemName.end(),originalItemName.begin(),::toupper);
if(itemName.compare(originalItemName) == 0){
searchHit = true;
cout << left << setw(9) << shipments[i].foodName << " | "
<< right <
<< right <
<< right <
<< right <
<< right <
<< right << fixed << setprecision(2) << setfill('0') << shipments[i].priceofitem << " "
<< setfill(' ') << endl;
cout.unsetf(ios_base::fixed);
}
}
if(!searchHit){
cout << "No Items Found in Shipments! Try Again" << endl;
}
}

Add a comment
Know the answer?
Add Answer to:
Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help writing a program that meets pseudocode and criteria . Txt File below input.txt file...

    Need help writing a program that meets pseudocode and criteria . Txt File below input.txt file data 05 11/30/16 03 12/07/16 05 12/07/16 05 12/08/16 01 12/10/16 07 12/11/16 07 12/14/16 06 12/15/16 02 12/21/16 05 12/21/16 06 12/22/16 07 12/22/16 08 12/23/16 07 12/23/16 07 12/23/16 07 12/23/16 08 12/24/16 08 12/24/16 07 12/24/16 03 12/26/16 05 12/26/16 07 12/28/16 04 12/29/16 07 01/01/17 06 01/03/17 07 01/03/17 08 01/05/17 05 01/10/17 04 01/17/17 08 01/17/17 07 01/18/17 07...

  • How do i find the image height and width in SOF marker Segment( the Xthumbnail and...

    How do i find the image height and width in SOF marker Segment( the Xthumbnail and Ythumbnail are all 0)? Which is the resolution of the image ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 c8 00 c8 00 00 ff db 00 43 00 02 02 02 02 02 01 02 02 02 02 02 02 02 03 03 06 04 03 03 03 03 07 05 05 04 06 08 07...

  • In a digital communication system, probability density function of the two level signal received in the...

    In a digital communication system, probability density function of the two level signal received in the receiver is: PR(v) = PS(v)*PN(v) = [0.4δ(v+1) + 0.6δ(v-4)]*η(v). And , η(v) is the noise that added to the message sign as the additive Gaussian noise with a value of zero and an effective value of 3. (* symbol means convolution process, in the solution of this problem you can use the below Q function table.) ,   η(v) = A) Plot the probability density...

  • Nonlinear. Here we want you to verify this property by computing the output of S, for the followi...

    nonlinear. Here we want you to verify this property by computing the output of S, for the following two pairs of inputs. The S-box S is given as follows: 0-6. (10 points) One important property which makes DES secure is that the S-Boxes are S-box S S0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 14 04 13 01 02 15 11 08 03 10 06 12 05 09 00 07 1...

  • P12-5 a,b,c,d please solve P12-5 a,b,c,d by referring P12-4 P12-5 a,b,c,d What is the economic value...

    P12-5 a,b,c,d please solve P12-5 a,b,c,d by referring P12-4 P12-5 a,b,c,d What is the economic value of the bonds on December 31,2001? CHAPTER 12 LONG-TERM LIABILITIES g. What should the liability value and the economic value of the bonds be on January 1, 2011, the maturity date of the bonds? Explain. 213 oh. Prepare the entry to record the retirement of the bonds on January 1,2011. P12-5 Refer to P12-4. Assume that on June 30, 2009, market interest rates soared...

  • The data set below are the unemployment rates from April 2016 through March 2018. The mean...

    The data set below are the unemployment rates from April 2016 through March 2018. The mean unemployment rate is 4.5, the median unemployment rate is 4.5, and the standard deviation of 0.33. Describe the data set using these statistics. Does the data appear to be consistent on a monthly basis? Year/Month Unemployment rate 2016-04 5.0 2016-05 2016-06 2016-07 2016-08 2016-09 2016-10 2016-11 2016-12 2017-01 2017-02 2017-03 2017-04 MacBook Pro 2017-05 4.3 2017-06 4 2017-07 4.3 2017-08 4.4 2017-09 4.2 2017-10...

  • Random Sampling Written Homework 1. The table below lists students in an imaginary Statistics class, along...

    Random Sampling Written Homework 1. The table below lists students in an imaginary Statistics class, along with their identification numbers. Use your graphing calculator to select a random sample of 8s 01. Adrian 07, Gregg 08. Helern 13. Max 19. Stella Adrian07 Greg8 02. Betty 03. Carl 04. Diane 05. Earl ET11. Kar 14. Nicole 15. Oliver 16. Paxton 20. Trace 21. Una 22. Verle 12. Lucy 18. Rashid 17. Quentin 23. Wyatt 24. Xavier 06. Filene 2. The same...

  • The file containing the JAVA files, pseudocode file and doc file that have written for this...

    The file containing the JAVA files, pseudocode file and doc file that have written for this lab. Preamble The file releasedates.txt contains a list of video games and their release dates. Each line of the file contains the release date, a tab character, and then the name. The list is currently totally unsorted. The object of today's lab is to write a series of methods that allow us to perform the following tasks: read contents from a file and store...

  • The code should work exactly the same as given example. The files: ---------------------------------------------------...

    The code should work exactly the same as given example. The files: ---------------------------------------------------------------------------------------------------- -------- FILE NAME ------- album0 ---------- FILE ------- <album> 323 Licensed to Ill Beastie Boys 25.0 </album> <album> 70 Enter the Wu_Tang: 36 Cham... Wu Tang Clan 25.99 </album> turning to Alice, she went on, `What's your name, child?' and there stood the Queen in front of them, with her arms folded, <album> 115 Daydream Nation Sonic Youth 5.0 </album> <album> 414 52nd Street Billy Joel 25.75...

  • Use the following tide table to answer the questions in the Assessing Your Learning section of...

    Use the following tide table to answer the questions in the Assessing Your Learning section of this lab. This tide table shows the predicted ocean heights for Santa Cruz/Monterey Bay, California during December 2011. 1. A visit to Santa Cruz/Monterey Bay, California is planned during the week of December 4-10 to view the tide pools. To observe intertidal animals during the daytime at the lowest possible tide, the best time to visit the beach is on _________________ at _________________. (4...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT