Need help asap plz

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;


while(1)
{

cout<<endl;
cout<< "+-------------------------------------+" <<endl;
cout<< "| Main Menu: |" << endl;
cout<< "| -------------- |" << endl;
cout<< "| |"<< endl;
cout<< "| 1) Load |" << endl;
cout<< "| 2) Add |" << endl;
cout<< "| 3) Search |" << endl;
cout<< "| 4) Print |" << endl;
cout<< "| 5) Save |" << endl;
cout<< "| 6) Exit |" << endl;
cout<< "+-------------------------------------+" <<endl;
cout<<endl;
cout << "Enter your choice:";
cin >> choice;

switch(choice)
{

{
case 1:

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);

inventoryNumber.push_back(number);
sort(inventoryNumber.begin(), inventoryNumber.end());

cout << "Enter an item description: ";
cin.ignore();
getline(cin, description);

inventoryDescription.push_back(description);
sort(inventoryDescription.begin(), inventoryDescription.end());

cout << "Enter the quantity: ";
cin>>quantity;

cout << "Enter an item price: ";
cin >> price; cout<< " $ ";

total=quantity*price;
myFile2 << number << endl;
myFile2 << description << endl;
myFile2 << quantity << endl;
myFile2 << price;
myFile2 << total <<endl;
int invNumS;

myFile2.close();
break;
}
case 3://search

int invNumS;

cout << "Enter the inventory number of the item to search: ";
cin>> invNumS;


for (vector<string>::iterator i = inventoryNumber.begin(); i != inventoryNumber.end(); i++)
{
if (invNumS == inventoryNumber.size())
{
cout << "Data for item number : " << invNumS<< endl;
cout << "Item description : " << inventoryDescription.size() << endl;
cout << "Quantity: " << quantity<<endl;
cout << "Price: " << price << " $ "<< endl;
cout<<"Total: " <<total<<endl;
}

else
{
cout << "There is no item number " << invNumS<<endl;
}
}

break;



case 4:
{

}
case 5:
{

}
case 6:
{
exit(0);
}
default:
{
cout << "You entered an invalid option" << endl;
}
}


}
}
Last edited on
I am not getting this yet.. can u explain more how ofstream would help me solve this problem..
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.

Remember to call close() on files.
Last edited on
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;


while(1)
{

cout<<endl;
cout<< "+-------------------------------------+" <<endl;
cout<< "| Main Menu: |" << endl;
cout<< "| -------------- |" << endl;
cout<< "| |"<< endl;
cout<< "| 1) Load |" << endl;
cout<< "| 2) Add |" << endl;
cout<< "| 3) Search |" << endl;
cout<< "| 4) Print |" << endl;
cout<< "| 5) Save |" << endl;
cout<< "| 6) Exit |" << endl;
cout<< "+-------------------------------------+" <<endl;
cout<<endl;
cout << "Enter your choice:";
cin >> choice;

switch(choice)
{

{
case 1:

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 << "File Not Found";
exit(1);
}
else
{
cout << "File has successfully loaded..." << endl << endl;
}

break;
}

case 2:

{
myFile2.open(fileName.c_str(),std::fstream::app);
string inputBuffer;
getline(cin, inputBuffer);
cout << "Enter inventory number for new data: ";
cin.ignore();
getline(cin, number);

inventoryNumber.push_back(number);
sort(inventoryNumber.begin(), inventoryNumber.end());

cout << "Enter an item description: ";
cin.ignore();
getline(cin, description);

inventoryDescription.push_back(description);
sort(inventoryDescription.begin(), inventoryDescription.end());

cout << "Enter the quantity: ";
cin>>quantity;

cout << "Enter an item price: ";
cin >> price;

total=quantity*price;
myFile2 << number << endl;
myFile2 << description << endl;
myFile2 << quantity << endl;
myFile2 << price <<endl;
myFile2 << total <<endl;
int invNumS;

myFile2.close();
break;
}
case 3:

int invNumS;

cout << "Enter the inventory number of the item to search: ";
cin>> invNumS;


for (vector<string>::iterator i = inventoryNumber.begin(); i != inventoryNumber.end(); i++)
{
if (invNumS == inventoryNumber.size())
{
cout << "Data for item number : " << invNumS<< endl;
cout << "Item description : " << inventoryDescription.size() << endl;
cout << "Quantity: " << quantity<<endl;
cout << "Price: " << price << " $ "<< endl;
cout<<"Total: " <<total<<endl;
}

else
{
cout << "There is no item number " << invNumS<<endl;
}
}

break;



case 4:
{

}
case 5:
{

}
case 6:
{
exit(0);
}
default:
{
cout << "You entered an invalid option" << endl;
}
}


}
}
Last edited on
Topic archived. No new replies allowed.