However, the problem is that whenever I exit and later want to resume, the code deletes the previous information and I have to start over again. How do I modify my code to continue from where I left off in the data? My code is list below, however, the print function under the materialList class is what I want to modify.
#include<iostream>
using std::cerr;
using std::endl;
#include <list>
usingnamespace std;
#include <fstream>
using std::ofstream;
#include <cstdlib> // for exit function
#include <sstream>
usingnamespace std;
class Item{
//Access specifer
public: //todo, private with get/set
string item;
int meshNum;
int mNum;
//constructor
public:
Item( string item,int mNum, int meshNum ){
this->item=item;
this-> mNum= mNum;
this-> meshNum= meshNum;
}
//Memeber functions
public:
string getItem(){
return item;
}
void setItem(string item){
this->item = item;
}
int getMeshNum(){
returnthis->meshNum;
}
void setMeshNum(int meshNum){
this->meshNum= meshNum;
}
int getMNum(){
returnthis->mNum;
}
void setMNum(int mNum){
this-> mNum= mNum;
}
};
//____________________________________________
class materialList{
// Access specifer
private:
list <Item> items;
//constructor
public:
/* materialList(){
this->items = new list<Item>;
} */
// Memeber fucntions
public:
void add( const Item& item)
{
items.push_back(item);
}
//print my list
void Print()
{
ofstream outdata; // outdata is like cin
outdata.open("example2.dat"); // opens the file
if( !outdata ) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
for (auto &i : items)
outdata << i.getItem() << " "<<i.getMeshNum()<< " "<<i.getMNum()<<endl;
outdata.close();
}
void search(ifstream& inFile){
string line;
int word;
int materialNum;
istringstream iss;
cout << "Enter a material number:";
cin >> materialNum;
int i=0;
while(!inFile.eof()){
// read line by line from the file
getline(inFile,line);
if(inFile.good()){
// read word by word in line and place words in arr
iss.clear(); // clear out state
iss.str(line);
iss >> word;
}
if (word == materialNum){
cout << line << endl;
}
}
}
};