It will not add Inventory to the program. It prints out zeroes for the list. Please someone help me make this work
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
struct Inventory
{
long Inventory_N;
float Price;
char Inventory_Name[50];
};
void menu (ifstream&, ofstream& , Inventory L[], int);
void loadInventory(ifstream&, Inventory L[], int);
void addInventory(ofstream& , Inventory L[], int);
int searchInventory (Inventory L[], int , int );
void printHead(ofstream& fout);
void listInventory (ofstream&, Inventory L[], int);
void saveInventory(ofstream&, Inventory L[], int);
int main()
{
int amount;
cout<<"Welcome to the Inventory Management System (IMS)!"<<endl<<endl;
cout<<"How many Inventory items is on the file that contains the info on the inventory?";
cin>>amount;
string filename;
Inventory *Info;
int Num = amount;
Info = new Inventory[Num]; /// create a new array of the struct into memory ///
ifstream input; /// for reading input ///
ofstream output(filename.c_str()); /// for output file ///
/// set format ///
output << fixed << showpoint << left << setprecision(2);
/// start of main menu ///
menu(input, output, Info, Num);
/// close all files ///
input.close();
output.close();
delete Info;
return 0;
}
void menu (ifstream& fin, ofstream& fout, Inventory L[], int size)
{
int pick = int();
int number = int();
int place = int();
while (pick != 6){
cout<<endl;
cout<<"To access a feature, enter one of the numbers listed below: "<<endl<<endl;
cout<<"1 - Load the Inventory."<<endl;
cout<<"2 - Add an Inventory Item."<<endl;
cout<<"3 - Search the Inventory for an item."<<endl;
cout<<"4 - List the Inventory items on the screen "<<endl;
cout<<"5 - Save the Inventory"<<endl;
cout<<"6 - Exit the IMS"<<endl;
cout<<"Pick a feacture to access: ";
cin>>pick;
switch(pick){
case 1://load
loadInventory(fin, L, size);
cout << "Press Enter to continue." << endl;
cin.get(); cin.get(); /// pause until enter key ///
menu (fin, fout, L, size); /// back to main menu //
break;
case 2://add
addInventory(fout, L, size);
cout << "Press Enter to continue." << endl;
cin.get(); cin.get(); /// pause until enter key ///
menu (fin, fout, L, size); /// back to main menu //
break;
case 3://search
cout << "Enter the inventory number of the item to search: ";
cin>> number;
place = searchInventory (L, size, number);
if (place == -1)
cout<<"Searched item not on the list."<<endl;
cout << "Press Enter to continue." << endl;
cin.get(); cin.get(); /// pause until enter key ///
menu (fin, fout, L, size); /// back to main menu //
break;
case 4://list
printHead(fout);
listInventory (fout, L, size);
cout << "Press Enter to continue." << endl;
cin.get(); cin.get(); /// pause until enter key ///
menu (fin, fout, L, size); /// back to main menu //
break;
case 5://save
saveInventory(fout, L, size);
cout << "Press Enter to continue." << endl;
cin.get(); cin.get(); /// pause until enter key ///
menu (fin, fout, L, size); /// back to main menu //
break;
case 6://exit
cout<<"Thank you...Good Bye!"<<endl;
return;
break;
default:
cout<<"Invalid entry!"<<endl;
break;
}
}
}
void loadInventory(ifstream& fin, Inventory L[], int size)
{
string filename;
cout<<"Enter the filename that has the inventory stored: "<<endl;
cin>> filename;
fin.open(filename.c_str());
if (fin.fail()) {
cerr << "The file "<<filename<<" is not found.";
}
else{
cout<<filename<<" opened successfully "<<endl;
}
/// loop until end of array ///
int i;
for(i = 0; i < size; i ++){
fin >> L[i].Inventory_N;
fin >> L[i].Price;
fin.get(L[i].Inventory_Name, 50); /// get whole name ///
fin.ignore (10, '\n'); /// clear all values ///
}
/// number of records found ///
cout << "The program found " << i << " records." << endl;
}
void addInventory(ofstream& fout, Inventory L[], int size)
{
string filename;
fout.open(filename.c_str(), ios::app);
cout << "Enter inventory number for new data: ";
long Inventory_N;
cin>> Inventory_N;
cout<< endl << "Enter the price for this item: $";
float Price;
cin>>Price;
cout<< endl << "Enter the inventory name for this item: ";
char Inventory_Name[50];
cin>> Inventory_Name;
fout<<Inventory_N<<endl;
fout<<Price<<endl;
fout<<Inventory_Name<<endl;
fout.flush();
fout.close( );
}
int searchInventory (Inventory L[], int size, int find)
{
for (int i = 0; i < size; i++)
if (find == L[i].Inventory_N){
cout << endl << "The price of the Inventory '"
<< L[i].Inventory_N << "' is $" << L[i].Price;
cout << endl << "The inventory name of the Inventory '"
<< L[i].Inventory_N << "' is " << L[i].Inventory_Name;
}
return (1);
}
void printHead(ofstream& fout)
{
for (int i = 0; i < 97; i++){
fout << "-";
}
for (int i = 0; i < 97; i++){
cout << "-";
}
fout << endl;
fout << setw(15) << "Inventory_number"<<'\t';
fout << setw(15) << "Price"<<'\t';
fout << setw(15) << "Inventory_name";
fout << endl;
for (int j = 0; j < 97; j++){
fout << "-";
}
cout << endl;
cout << endl;
cout << setw(15) << "Inventory_number"<<'\t';
cout << "Price"<<'\t';
cout << setw(15) << "Inventory_name";
cout << endl;
for (int j = 0; j < 97; j++){
cout << "-";
}
cout << endl;
}
void listInventory (ofstream& fout, Inventory L[], int size)
{
string filename;
cout<<"What name would you like the file to be saved as? ";
cin>> filename;
fout.open(filename.c_str());
for (int i = 0; i <size; i++){
fout << setw(15) << L[i].Inventory_N<<'\t';
fout << "$";
fout << setw(15) << L[i].Price<<'\t';
fout << setw(15) << L[i].Inventory_Name;
fout << endl;
cout << setw(15) << L[i].Inventory_N<<'\t';
cout << "$";
cout << setw(15) << L[i].Price<<'\t';
cout << setw(15) << L[i].Inventory_Name;
cout << endl;
}
}
void saveInventory(ofstream& fout, Inventory L[], int size)
{
string filename = string();
cout<<"What name would you like the file to be saved as? ";
cin>> filename;
fout.open(filename.c_str());
for(unsigned i = 0; i < size; i ++){
fout << L[i].Inventory_N;
fout << L[i].Price;
fout << L[i].Inventory_Name;
}
cout<< endl << "Inventory is saved. "<<endl;
fout.close();
}