Write your question here.
I have a code that need help completing the forth and fifth of it.. Also I have issue with the the second and third case, once i add an inventory item, the old inventory item gets deleted from the file. Also, when i search using inventory item, it says its not found..
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
void listInventory (ofstream& myFile2, string inventoryArray[], int sizeArray[]);
int choice;
string fileName;
ifstream myFile;
ofstream myFile2;
int invNumS;
vector<string> inventoryDescription;
vector<string> inventoryNumber;
vector<string>::const_iterator it;
string description;
string number;
int size = 0;
double price;
int quantity;
double total;
cout << "Please, enter the name of the file you would like to open? ";
cin >> fileName;
cout << endl;
myFile.open(fileName.c_str());
if (myFile.fail())
{
cerr << "Open failed";
exit(1);
}
else
{
cout << "File has successfully loaded, you may now return to the menu." << endl << endl;
}
break;
}
case 2://add
{
myFile2.open(fileName.c_str());
cout << "Enter inventory number for new data: ";
cin.ignore();
getline(cin, number);
Check parameter part, ofstream can have 2 parameter. First one is name file and second one is mode, as far as i can remember if mode is not set its automaticly become trunc in this mode your file will be cleared before add anything. You can solve it using append mode
The default openmode for ostreams is out. Out leaves the old content intact, but when the file opens you will start from the beginning and write operations overwrite the content.
You should open the file only in case 1. The others should work with the file previously opened.
okay I just looked over the part I am missing in ofstream but wondering is that what u guys were trying to say??, I kinda done what u guys said but figure out that my input are missing first letter in the output file..
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
void listInventory (ofstream& myFile2, string inventoryArray[], int sizeArray[]);
int choice;
string fileName;
ifstream myFile;
ofstream myFile2;
int invNumS;
vector<string> inventoryDescription;
vector<string> inventoryNumber;
vector<string>::const_iterator it;
string description;
string number;
int size = 0;
double price;
int quantity;
double total;