1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
// The program will read an inventory file into 3 parallel arrays.
// One array will contain strings for Item Description
// One array will contain doubles for Cost Per Item
// One array will contain doubles for No. of Items and item Shippable status.
// The program will present a menu presents a user with 4 choices:
// 1. Read the inventory file, 2. Display the Inventory,
// 3. Write the inventory to a file, 4. Exit the program.
// The program should validate user input for a valid menu option.
// The program should not continue if the file is not read.
// 06APR2022 Group 2
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <string>
using namespace std;
// Function prototypes.
void logo(); // This function outputs the company logo.
void menu(); // This function outputs the menu.
bool read_and_display_file(); // test read of external file by calling a function
void displayInventory(string names[], double cost[], int ship[][2])
int main()
{
// Declare constants and variables.
int userChoice; // hold user's menu selection
string itemNames; // represents item description content from input file
ifstream inputFile; // inventory file - is located in function read_file
do
{
// Step 2. Call the logo function to output the company logo.
logo();
// Call the menu function to display the menu.
menu();
// Input user selection.
cin >> userChoice;
cin.ignore(2, '\n'); // To prevent program from storing beyond the first digit entered
// exit(0); // just for testing
switch (userChoice)
{
case 1:
{
// Testing - only the Item Descriptions are included in the test file
// Open the file and output all lines.
// If successful, then break it down into the 3 arrays per the Mod 6 assignment
read_and_display_file();
break;
}
case 2:
cout << "You chose " << userChoice << endl;
void displayInventory(string names[], double cost[], int ship[][2])
break;
case 3:
cout << "You chose " << userChoice << endl;
break;
case 4:
cout << "\nGood bye!\n" << endl;
break;
default:
cout << "Invalid Menu Item entered...must enter 1-4...\n";
}
} while (userChoice != 4);
return 0;
}
//*****************************************************************
// This function outputs the company logo. *
//*****************************************************************
void logo()
{
cout << "\n *** FUN FOODS FAST ***\n";
cout << "***************************************\n";
cout << "* ******* ******* ******* *\n";
cout << "* ******* ******* ******* *\n";
cout << "* ** ** ** *\n";
cout << "* ****** ****** ****** *\n";
cout << "* ****** ****** ****** *\n";
cout << "* ** ** ** *\n";
cout << "* ** ** ** *\n";
cout << "* ** ** ** *\n";
cout << "***************************************\n";
}
//*****************************************************************
// This function should generate the menu. *
//*****************************************************************
void menu()
{
// Step 2. Display the menu.
cout << "\n1. Read in Inventory\n"
<< "2. Display Inventory\n"
<< "3. Write to File\n"
<< "4. Exit\n";
}
//******************************************************************
// This function should read the external inventory file *
// and display it's contents *
//******************************************************************
bool read_and_display_file() // This is for testing to see if the file contents are read and displayed
{
ifstream inputFile; // inventory file
string itemNames;
inputFile.open("c:\\temp\\inputInventory.txt"); // Open the file.
if (inputFile) // Return true if file is found or opened
{
while (getline(inputFile, itemNames)) // Read all rows in the file
{
cout << endl << itemNames; // Display each row's content
}
cout << endl;
inputFile.close(); // Close the file.
return true;
}
else // Return false if file cannot be found or opened
{
cout << "\nFile could not be read.\n";
return false;
}
//******************************************************************
// This function should display the inventory file
// and display its contents
//******************************************************************
void displayInventory(string names[], double cost[], int ship[][2])
{
int count = 0; // counter value for loop
// Display column headings.
cout << "\nItem Name" << "\t\t"
<< "Cost" << "\t"
<< "No. Stock" << "\t"
<< "Shipping (1-Yes 0-No)\n";
// Formatting for consistent output of data type doubles.
cout << setprecision(2) << fixed << showpoint;
// Loop to dispaly values from the input file.
for (count = 0; count < 10; count++)
cout << names[count] << "\t"
<< setw(12) << cost[count]
<< setw(13) << ship[count][0]
<< setw(25) << ship[count][1] << "\n";
}
|