When I tried to run this code on c++ 17 it worked but then on c++ it doesn't. While running the first time it just gives me the string value of the last entered elements but when I run it the second time it shows the segmentation fault(core dumped) error. Can someone help me with this?
Here is my code:
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<iomanip>
#include <stdlib.h>
using namespace std;
void show1(string name,string type,string price,string quantity){
cout<<"=======================================\n";
cout<<setw(5)<<name<<setw(15)<<type<<setw(10)<<price<<setw(15)<<quantity<<"\n";
cout<<"========================================\n";
}
template<class T>
void Readfile(T &obj,string fname,string name,string type,string price,string quantity){
int i=1;
ifstream f;
f.open(fname,ios::in);
f.seekg(0);
Display(name,type,price,quantity);
while(!f.eof()){
f.read((char*)&obj, sizeof(obj));
if(!f.eof()) {
if(f.tellg()<0)
{ i=0; break;}
obj.putdata();
}
}
f.close();
}
class Items{
string name;
double price;
int quantity;
public:
string itemObj;
void getdata(string obj){
cout<<"Enter name, price and quantity: \n";
cin>>name>>price>>quantity;
itemObj=obj;
}
void putdata(){
cout<<setw(5)<<name<<setw(10)<<itemObj<<setw(12)<<price<<setw(10)<<quantity<<"\n";
}
void writeToFile(int n,string obj);
void ReadFile_item();
int check(string temp_name);
void refill_Item(int qty);
double checkout_price(int qty);
}item;
class CD:public Items{
public:
void Add_CD();
}cd;
double Items::checkout_price(int qty){
if(quantity>=qty){
quantity-=qty;
cout<<"File Updated";
return price*qty;
}
else{
cout<<"\nNot enough stock";
return 0;
}
}
void Items::writeToFile(int n,string obj){
ofstream f;
f.open("stock.txt",ios::out|ios::app);
for(int i=0;i<n;i++){
item.getdata(obj);
f.write((char*)&item, sizeof(item));
cout<<"File Updated\n";
}
f.close();
}
void Items:: ReadFile_item(){
Readfile(item,"stock.txt","Name","Item type"," Price ","Available stock");
}
void Add_item_obj(string obj){
int n;
cout<<"Enter no. of "<<obj<<"'s to add: \n";
cin>>n;
item.writeToFile(n,obj);
}
void CD::Add_CD(){
Add_item_obj("CD");
}
void main_menu(){
int i=0;
do{
cout<<"============================\n";
cout<<"-------MAIN MENU------------\n";
cout<<"============================\n";
cout<<"1. Sell Items\n";
cout<<"2. Add Items\n";
cout<<"4. View stock file\n";
cout<<"5. Exit \n";
cout<<"Enter your choice: ";
cin>>i;
switch(i){
case 1: sell_items();
break;
case 2: writetoFile(1,"CD");
break;
case 3: item.ReadFile_item();
break;
default: cout<<"Exiting... \n";
exit(0);
}
}while(i!=0);
}
int main(){
main_menu();
return 0;
}