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

I feel completely lost on this subject. Using c++ and visual studios 2017. Need

ID: 3859877 • Letter: I

Question

I feel completely lost on this subject. Using c++ and visual studios 2017. Need help with a project where we are editing our previous project. I will paste the description and the code below. If needed, I will add the information for my input .txt at the very bottom. Sorry if it's a lot. Thank you for your help.

#include<iostream> // Lines 5 - 10 are self explanatory.

#include<fstream>

#include<iomanip>

#include <string>

using namespace std;

bool loadArrays(const char fileName[], long idArray[], int storeArray[], int qtyArray[], int & count, int maxCells); // Lines 12 - 14 are function declarations.

void printArrays(ostream & w, const long idArray[], const int storeArray[], const int qtyArray[], int count); // I will avoid explaing them here, since you're most likely going to inspect the

bool extractData(const char newFileName[], long requestId, int baseQty, const long idArray[], // definition anyways.

const int storeArray[], const int qtyArray[], int count, int & newCount);

int main() // Start of main.

{

const int MAX_CELLS = 30; // Lines 19 - 29 are declared variables and arrays. Also have some initialized values.

char newFileName[100] = "ExtractData.txt";

char fileName[100] = "StoreData.txt";

long requestId;

const int ORDER_VALUE = 500;

long idArray[MAX_CELLS];

int storeArray[MAX_CELLS];

int qtyArray[MAX_CELLS];

int count = 0;

int newCount = 0;

bool result;

result = loadArrays(fileName, idArray, storeArray, qtyArray, count, MAX_CELLS); // Function call - result takes the returned value from our function. In this case, true or false.

if (result == false) // If result has a false value.

{

if (count == 0) // If statement used to generate error codes for certain situations.

{

cout << "Could not open input file " << fileName << endl; // Prints out to the user descriptive sentence.

system("PAUSE"); // Allows the console to remain open so we can see the error that occured.

exit(1); // Exit with error code.

}

else //Print error message about could not open file, abort

{

cout << "Could not load all data from the file: " << fileName; // Prints out descriptive sentence to the user.

system("PAUSE"); // Allows the console to remain open so we can see the error that occured.

exit(2); // Exit with different error code.

}

}

printArrays(cout, idArray, storeArray, qtyArray, count); // Function call - Used to print out to the screen our files information.

cout << "Please enter a product Id" << endl; // Prompt user.

cin >> requestId; // User inputs desired ID number.

result = extractData(newFileName, requestId, ORDER_VALUE, idArray, storeArray, qtyArray, count, newCount); // Function call - Outputs to a file the data of the ID number you inputted.

if (result == false) // If result is equal to false.

{

cout << "Could not open output file: " << newFileName; // Error message.

exit(3); // Exit with error code.

}

cout << "There were " << newCount << " lines extracted." << endl; // Descriptive message - tells us how many lines had that same ID number.

system("PAUSE"); // Allows the console to remain open so we can see the results.

return 0; // Indicates successful termination.

} // End of main.

bool loadArrays(const char fileName[], long idArray[], int storeArray[], int qtyArray[], int & count, int maxCells) // Function definition.

{ // Function is used to check and load in our file. If everything is in order, then files inputs are stored in the appropriate arrays.

bool okay = true; // Variable for our returned value. We assume true, unless our test find something wrong with our input file.

ifstream inFile("StoreData.txt", ifstream::in); // Input file we want read in.

if (inFile.fail()) // Test to see if loading the input file failed.

{

cout << "Could not open file." << endl; // Error message for if the input file failed the test.

exit(1); // Exit with error code.

}

if (!inFile) // Another test to make sure our file is inputted correctly.

okay = false; // Sets boolean value to false if it is true.

else

{

int i = 0; // Used to initialize our arrays.

inFile >> idArray[i] >> storeArray[i] >> qtyArray[i]; // Arrawys used to store our information from the input file.

while (inFile) // While we are still reading the input file - this will be true.

{

i++; // Increments.

inFile >> idArray[i] >> storeArray[i] >> qtyArray[i]; // I increments so that way each array number has it's own values from the input.

}

count = i - 1; // Helps keep track of how many lines are in the input file, and we use this value in our other functions.

if (!inFile.eof()) // If we don't reach the end of the file, then we are returned with a false value.

okay = false; // False value for okay.

inFile.close(); // Close the input files.

}

return okay; // Returns our okay value to the variable result.

} // End of function.

void printArrays(ostream & w, const long idArray[], const int storeArray[], const int qtyArray[], int count) // Function definition.

{ // Used to print out our input files values to the screen.

for (int i = 0; i <= count; i++) // For loop, uses the count value from the loadArray function to determine how many times needed to iterate.

{

w << idArray[i] << " " << storeArray[i] << " " << qtyArray[i] << endl; // Prints out to the screen the input files information stored in the arrrays.

}

} // End of function.

bool extractData(const char newFileName[], long requestId, int baseQty, const long idArray[], const int storeArray[], const int qtyArray[], int count, int & newCount) // Function definition.

{ // Function used to extract the desire ID number's input values to our output file.

bool okay = true; // Set to true as default.

ofstream outFile("ExtractData.txt", ofstream::out); // Connects to outsream and output file.

if (outFile.fail()) // Test to see if it's working.

{

cout << "Could not open file." << endl; // If test failed, we get this error message.

exit(1); // Exit with error code.

}

if (!outFile) // Another test.

okay = false; // If condition is true, then our boolean value is changed.

else

{

for (int i = 0; i < count; i++) // For loop to help us iterate the if statement. Count is determine by the value in loadArrays function.

{

if ((idArray[i] == requestId) && (qtyArray[i] < baseQty)) // Test used to determine if the program need to keep printing information and updating the counter, or not.

{

outFile << idArray[i] << " " << storeArray[i] << " " << qtyArray[i] << " "; // Printing information in the file.

newCount++; // Used to help us know how many lines of data were extracted. Hence, lines with the same ID number.

}

}

outFile.close(); // Close our output file.

}

return okay; // Returns a boolean value.

} // End of function.

StoreData.txt

11111 1 7
22222 1 2
33333 1 11
44444 1 8
11111 2 4
22222 2 5
33333 2 6
44444 2 9
11111 3 12
22222 3 22
33333 3 5
44444 3 14
11111 4 1
22222 4 3
33333 4 21
44444 4 5
11111 5 9
22222 5 10
33333 5 16
44444 5 19

1. Rewrite Project 3 using an array of objects, rather than the three parallel arrays of Project 3. Create a class named inventory, there are 3 data fields: itemld, storeNr and quantity. Design the necessary header file and implementation file. 2 Writ three + functions as deseribed below The variables listed below have the following meaning: fileName -the Windows name of the file created in step 1 newFileName the Windows file name of the extracted records requestld the product id number used to extract data ORDER VALUE -the quantity value used to extract data, make this a global constant with a value of 500 imvList -the array of inventory object nvList the array of inventory objects count the actual number of cells filled in the array newcount -the number of extracted records written to newFileName

Explanation / Answer

parallel.cpp

#include <iostream>
#include <fstream>

using namespace std;

const int ORDER_VALUE = 500;
const int MAX_SIZE = 30;

// loads 3 arrays from file and use count to load cell dimensions
bool loadArrays(const char fileName[], int idArray[], int storeArray[], int qtyArray[], int &count, int maxCells);

// prints location of data in array and number of cells filled
void printArrays(ostream &where, const int idArray[], const int storeArray[], const int qtArray[], int count);

// loads data specifically from requestID provided
bool extractData(const char newFileName[], int requestId, int baseQty, const int idArray[], const int storeArray[], const int qtArray[], int count, int &newcount);

int main()
{
   int idProduct[MAX_SIZE];
   int storeNumber[MAX_SIZE];
   int quantity[MAX_SIZE];

   int howMany = 0; // how many cells loaded from original file
   int newCount = 0; // the number of extracted records written to new file
   int searchID; // product ID

   char fileName[MAX_SIZE];
   char newFileName[MAX_SIZE];

   cout << "Enter the name of the file to read: ";
   cin >> fileName;

   // change last parameter to 10 to test where no room for all data
   if (loadArrays(fileName, idProduct, storeNumber, quantity, howMany, 10))
   {
       cout << "All data loaded." << endl;
   }
   else
   {
       cout << "Unable to load all of the records." << endl;
   }

   cout << howMany << " cells loaded." << endl << endl;

   printArrays(cout, idProduct, storeNumber, quantity, howMany);

   cout << endl;
   cout << "Please enter new file name: ";
   cin >> newFileName;
   cout << "Please enter product ID number: ";
   cin >> searchID;

   extractData(newFileName, searchID, ORDER_VALUE, idProduct, storeNumber, quantity, howMany, newCount);
  
   cout << newCount << " cells written to file." << endl;

   return 0;
}

bool loadArrays(const char fileName[], int idArray[], int storeArray[], int qtyArray[], int &count, int maxCells)
{
   ifstream in;
   in.open(fileName);

   if (!in)
   {
       cout << "Invalid file. Please try again." << endl;
       exit(1); // terminate program
   }

   int i = 0;
   while ((i < maxCells) && (in >> idArray[i] >> storeArray[i] >> qtyArray[i]))
   {
       i++;
   }
   count = i;

   // check if there is more data
   if (in.good())
   {
       return false;
   }
  
   in.close();
   return true;
}

void printArrays(ostream &where, const int idArray[], const int storeArray[], const int qtArray[], int count)
{
   where << "Product " << "Store " << "Quantity" << endl;
   for (int i = 0; i < count; i++)
   {
       where << idArray[i] << " " << storeArray[i] << " " << qtArray[i] << endl;
   }
}

bool extractData(const char newFileName[], int requestId, int baseQty, const int idArray[], const int storeArray[], const int qtArray[], int count, int &newcount)
{
   ofstream outfile;
   outfile.open(newFileName);

   if (!outfile.is_open())
   {
       cout << "Unable to open file." << endl;
       return false;
   }

   outfile << "Product ID " << requestId << endl;
   outfile << "Stores " << "Quantity" << endl;

   for (int i = 0; i < count; i++)
   {
       if (idArray[i] == requestId && qtArray[i] < baseQty)
       {
           outfile << storeArray[i] << " " << qtArray[i] << endl;
           newcount++;
       }
   }

   outfile.close();
   return true;  
}

parallel.txt
34230 1425 46
24098 1425 94
13133 1425 12
23246 1090 23
59324 1090 58
13133 3802 78
13133 3802 47
83473 3802 38
42424 3802 43
35020 1314 85
34230 1314 10
42424 1458 0
59324 1458 66
24098 3529 53
83473 3529 58
64837 5032 18
34230 5032 22
23246 5032 30
74537 4585 14
42424 4585 25

sample output

Enter the name of the file to read: parallel.txt
All data loaded.
20 cells loaded.

Product Store   Quantity
34230   1425    46
24098   1425    94
13133   1425    12
23246   1090    23
59324   1090    58
13133   3802    78
13133   3802    47
83473   3802    38
42424   3802    43
35020   1314    85
34230   1314    10
42424   1458    0
59324   1458    66
24098   3529    53
83473   3529    58
64837   5032    18
34230   5032    22
23246   5032    30
74537   4585    14
42424   4585    25

Please enter new file name: output.txt
Please enter product ID number: 42424
3 cells written to file.
Press any key to continue . . .

--------- Second execution ------------------
Enter the name of the file to read: parallel.txt
Unable to load all of the records.
10 cells loaded.

Product Store   Quantity
34230   1425    46
24098   1425    94
13133   1425    12
23246   1090    23
59324   1090    58
13133   3802    78
13133   3802    47
83473   3802    38
42424   3802    43
35020   1314    85

Please enter new file name: output2.txt
Please enter product ID number: 42424
1 cells written to file.
Press any key to continue . . .

main.cpp

#include <iostream>
#include <fstream>
#include "inventory.h"

const int ORDER_VALUE = 500; // quantity value used to extract data
const int MAX_ARR_SIZE = 25; // max array size
const int MAX_FILENAME_LEN = 40; // max length for filename string

// will load data from the disk file and returns true if all data are loaded
bool loadArray(char fileName[], inventory invList[], int & count, int maxCells);

// prints the data in the array of objects to the stream "where"
void printArray(ostream & where, const inventory invList[], int count);

// will write all data with matching requestId and under quantity of baseQty to new file
bool extractData(char newFileName[], int requestId, int baseQty,
   const inventory invList[], int count, int & newcount);

int main()
{
   inventory invList[MAX_ARR_SIZE];

   int count = 0; // holds the number of cells found in original file
   int newcount = 0; // hold the number of extracted records written to new file
   int requestId;

   char fileName[MAX_FILENAME_LEN];
   char newFile[MAX_FILENAME_LEN];

   cout << "Enter the name of the file to be read: ";
   cin >> fileName; // inventory.txt

   // change last parameter to 10 to test when no room for all data
   if (loadArray(fileName, invList, count, MAX_ARR_SIZE))
   {
       cout << "All data loaded." << endl;
   }
   else
   {
       cout << "Insufficent storage. Unable to load all data." << endl;
   }

   cout << count << " record(s) found" << endl << endl;
   printArray(cout, invList, count);

   cout << endl;
   cout << "Enter new file name: ";
   cin >> newFile;
   cout << "Enter product ID to search for: ";
   cin >> requestId; // product id number

   extractData(newFile, requestId, ORDER_VALUE, invList, count, newcount);
   cout << newcount << " record(s) written to file." << endl << endl;

   return 0;
}

void printArray(ostream & where, const inventory invList[], int count)
{
   for (int i = 0; i < count; i++)
   {
       where << invList[i].getId() << " "
           << invList[i].getStoreNr() << " "
           << invList[i].getQuantity() << endl;
   }
}

bool loadArray(char fileName[], inventory invList[], int & count, int maxCells)
{
   ifstream file;
   file.open(fileName);

   if (!file)
   {
       cout << "Unable to open file." << endl;
       exit(1); // terminate program
   }

   count = 0;
   for (int i = 0; i < maxCells; i++)
   {
       int prod, store, qty; // temp variables
       file >> prod >> store >> qty;
       invList[i].setId(prod);
       invList[i].setStoreNr(store);
       invList[i].setQuantity(qty);
       count++; // tracks the number of cells loaded
   }

   // check if there is more data
   if (file.good())
   {
       return false;
   }

   file.close();

   return true;
}

bool extractData(char newFileName[], int requestId, int baseQty,
   const inventory invList[], int count, int & newcount)
{
   ofstream outfile;
   outfile.open(newFileName);

   if (!outfile.is_open())
   {
       cout << "Unable to open file." << endl;
       return false;
   }

   newcount = 0;
   for (int i = 0; i < count; i++)
   {
       if (invList[i].getId() == requestId && invList[i].getQuantity() < baseQty)
       {
           outfile << invList[i].getId() << " "
               << invList[i].getStoreNr() << " "
               << invList[i].getQuantity() << endl;
           newcount++;
       }
   }

   outfile.close();

   return true;
}


inventory.cpp
#include "inventory.h"

inventory::inventory()
{
   itemId = 0;
   storeNr = 0;
   quantity = 0;
}

inventory::inventory(int id, int store, int qt)
{
   itemId = id;
   storeNr = store;
   quantity = qt;
}

void inventory::setId(int id)
{
   itemId = id;
}

void inventory::setStoreNr(int store)
{
   storeNr = store;
}

void inventory::setQuantity(int qt)
{
   quantity = qt;
}

int inventory::getId(void) const
{
   return itemId;
}

int inventory::getStoreNr(void) const
{
   return storeNr;
}

int inventory::getQuantity(void) const
{
   return quantity;
}

inventory.h


#ifndef inventory_h
#define inventory_h
#include <iostream>

using namespace std;

/**
* Class Inventory represents an item. Inventory shows which store has the item and how many. */
class inventory
{
public:
   inventory();
   inventory(int id, int store, int qt);

   void setId(int id);
   void setStoreNr(int store);
   void setQuantity(int qt);

   int getId(void) const;
   int getStoreNr(void) const;
   int getQuantity(void) const;

private:
   int itemId;
   int storeNr;
   int quantity;
};
#endif

inventory.txt
34230 1425 46
24098 1425 94
13133 1425 12
23246 1090 23
59324 1090 58
13133 3802 78
13133 3802 47
83473 3802 38
42424 3802 43
35020 1314 85
34230 1314 10
42424 1458 0
59324 1458 66
24098 3529 53
83473 3529 58
64837 5032 18
34230 5032 22
23246 5032 30
74537 4585 14
42424 4585 25

sample output

Enter the name of the file to be read: inventory.txt
All data loaded.
25 record(s) found.

34230   1425    46
24098   1425    94
13133   1425    12
23246   1090    23
59324   1090    58
13133   3802    78
13133   3802    47
83473   3802    38
42424   3802    43
35020   1314    85
34230   1314    10
42424   1458    0
59324   1458    66
24098   3529    53
83473   3529    58
64837   5032    18
34230   5032    22
23246   5032    30
74537   4585    14
42424   4585    25
42424   4585    25
42424   4585    25
42424   4585    25
42424   4585    25
42424   4585    25

Enter new file name: all_data_loaded.txt
Enter ID to search for: 42424
8 record(s) written to file.

Press any key to continue . . .

--------------------------------
----- No room for all data -----
--------------------------------

Enter the name of the file to be read: inventory.txt
Insufficent storage. Unable to load all data.
10 record(s) found

34230   1425    46
24098   1425    94
13133   1425    12
23246   1090    23
59324   1090    58
13133   3802    78
13133   3802    47
83473   3802    38
42424   3802    43
35020   1314    85

Enter new file name: no_room.txt
Enter product ID to search for: 13133
3 record(s) written to file.