10 Replies - 346 Views - Last Post: 08 December 2019 - 01:21 PM Rate Topic: -----

#1 galko441   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 19

Problem with load/save File fstream

Posted 07 December 2019 - 07:54 AM

Hello , I have problem with fstream .
I want to store files automatically and when i try to load the file it shows me only 1 line .
For example

data.txt
2;1;1;1;1
3;1;1;1;1
1;1;1;1;1

Result:

1;1;1;1;1

I want too see all information
And i got only the third line. Can someone help me ?

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;
//=================================================================================================================================================


fstream f;
int countall = 0;


class Tehnika
{
public:
	virtual void print_Info() = 0;
	virtual double calc_Tax() = 0;
	virtual void save_Info() = 0;
protected:
	string marka;
	string model;
	double cena;
	int br;

};
//------------------------------------------------------------------------------------------------------------------------------------------------
class PC : public Tehnika
{
public:
	PC(string ime, string mod, double suma, int broika, int k, int p) {
		marka = ime;
		model = mod;
		cena = suma;
		br = broika;
		kutiq = k;
		per = p;

	}
	virtual void print_Info() {
		cout << "Kategoriq: Komputri " << endl;
		cout << "Marka: " << marka << endl;
		cout << "Model: " << model << endl;
		cout << "Bazova cena: " << cena << endl;
		cout << "Broi: " << br << endl;
		cout << "Tip kutiq: " << kutiq;
		if (kutiq == 1) cout << "\t" << "1 - Mini ATX" << endl;
		if (kutiq == 2) cout << "\t" << "2 - ATX" << endl;
		if (kutiq == 3) cout << "\t" << "3 - XL ATX" << endl;
		cout << "Tip periferiq: " << per;
		if (per == 1) cout << "\t" << "1 - Mishka" << endl;
		if (per == 2) cout << "\t" << "2 - Klaviatura" << endl;
		if (per == 3) cout << "\t" << "3 - Mishka i klaviatura" << endl;
		cout << "Kraina cena: " << calc_Tax() << endl;
	}
	virtual double calc_Tax() {
		double cena1 = cena * br;
		if (kutiq == 1) cena1 += 20;
		if (kutiq == 2) cena1 += 30;
		if (kutiq == 3) cena1 += 40;
		if (per == 1) cena1 += 20;
		if (per == 2) cena1 += 30;
		if (per == 3) cena1 += 50;
		return cena1;
	}
	virtual void save_Info()
	{
		f << "PC;" << marka << ";" << model << ";" << cena << ";" << br << ";" << kutiq << ";" << per << ";" << endl;
	}
private:
	double cena;
	int kutiq;
	int per;
};
//-------------------------------------------------------------------------------------------------------------------------------------------------
class Laptopi : public Tehnika
{
public:
	Laptopi(string ime, string mod, double suma, int broika, int in, double t) {
		marka = ime;
		model = mod;
		cena = suma;
		br = broika;
		inch = in;
		teglo = t;
	}
	virtual void print_Info() {
		cout << "Kategoriq: Laptopi " << endl;
		cout << "Marka: " << marka << endl;
		cout << "Model: " << model << endl;
		cout << "Bazova cena: " << cena << endl;
		cout << "Broi: " << br << endl;
		cout << "Razmer: " << inch;
		if (inch == 1) cout << "\t" << "1 - 13'" << endl;
		if (inch == 2) cout << "\t" << "2 - 15'" << endl;
		if (inch == 3) cout << "\t" << "3 - 17'" << endl;
		cout << "Teglo: " << teglo;
		cout << endl;
		cout << "Kraina cena: " << calc_Tax() << endl;
	}
	virtual double calc_Tax() {
		double cena1 = cena * br;
		if (inch == 1) cena1 += 0;
		if (inch == 2) cena1 += 100;
		if (inch == 3) cena1 += 200;
		return cena1;
	}
	virtual void save_Info()
	{
		f << "LP;" << marka << ";" << model << ";" << cena << ";" << br << ";" << inch << ";" << teglo << ";" << endl;
	}
private:
	double cena;
	int inch;
	double teglo;

};
//--------------------------------------------------------------------------------------------------------------------------------------------------
class Tableti : public Tehnika
{
public:
	Tableti(string ime, string mod, double suma, int broika, int g, int u)
	{
		marka = ime;
		model = mod;
		cena = suma;
		br = broika;
		trig = g;
		gps = u;
	}
	virtual void print_Info() {
		cout << "Kategoriq: Tableti " << endl;
		cout << "Marka: " << marka << endl;
		cout << "Model: " << model << endl;
		cout << "Bazova cena: " << cena << endl;
		cout << "Broi: " << br << endl;
		cout << "3G: " << trig;
		if (trig == 1) cout << "\t" << "1 - Ima" << endl;
		if (trig == 2) cout << "\t" << "2 - Nqma" << endl;
		cout << "GPS: " << gps;
		if (gps == 1) cout << "\t" << "1 - Ima" << endl;
		if (gps == 2) cout << "\t" << "2 - Nqma" << endl;
		cout << endl;
		cout << "Kraina cena: " << calc_Tax() << endl;
	}
	virtual double calc_Tax() {
		double cena1 = cena * br;
		if (trig == 1) cena1 += 40;
		if (trig == 2) cena1 += 0;
		if (gps == 1) cena1 += 60;
		if (gps == 2) cena1 += 0;
		return cena1;
	}
	virtual void save_Info()
	{
		f << "TK;" << marka << ";" << model << ";" << cena << ";" << br << ";" << trig << ";" << gps << ";" << endl;
	}
private:
	double cena;
	int trig;
	int gps;
};
//---------------------------------------------------------------------------------------------------------------------------------------------------
class queueProducts
{
public:
	queueProducts() {};
	void save_File();
	void load_File();
	void Add(Tehnika* obj) {
		opashka.push_back(obj);
		/*fstream f;
		f.open("d:\\data\\proba1.txt", fstream::out);
		f << opashka;
		*/

	}
	void Process() {
		if (opashka.empty()) { //.size==0
			return;
		}
		cout << "Obrabotka na poruchkata: " << endl;opashka[0]->print_Info();cout << endl;
		cout << "Duljimata suma ot klienta e " << opashka[0]->calc_Tax() << endl; //-> za ukazateli
		opashka.erase(opashka.begin());
		::countall;
	}
	void List() {

		cout << "Neobraboteni poruchki:" << endl << endl;
		for (int x = 0; x < opashka.size(); x++) {
			opashka[x]->print_Info();

		}
		cout << endl << "Broqt na neobrabotenite ot klienta poruchki e " << opashka.size() << endl;


	}
private:
	vector<Tehnika*> opashka;

};

void queueProducts::save_File() {
	f.open("data.txt", fstream::out);
	for (int x = 0; x < opashka.size(); x++) {
		opashka[x]->save_Info();
	}
	f.close();
}
void queueProducts::load_File() {

	f.open("data.txt", fstream::in);
	if (!f.is_open()) return;
	string ID, marka, model, cena, br, info1, info2;

	while (getline(f, ID, ';'))
	{
		
		
		if (ID.empty()) continue;
		else if (ID == "PC") {
			getline(f, marka, ';');
			getline(f, model, ';');
			getline(f, cena, ';');
			double cena1 = stod(cena);
			getline(f, br, ';');
			int br1 = stoi(br.c_str());
			getline(f, info1, ';');
			int info11 = stoi(info1.c_str());
			getline(f, info2, ';');
			int info22 = stoi(info2.c_str());
			cout << "Komputur:" << endl;
			cout << "\t" << "Marka: " << marka << "\t" << "Model: " << model << "\t" << "Cena: " << cena << "\t" << "Broi: " << br << "\t" << "Tip kutiq: " << info1 << "\t" << "Periferiq: " << info2;
			cout << endl << endl;


			Add(new PC(ID, model, cena1, br1, info11, info22));
		}
		if (ID == "LP") {


			getline(f, marka, ';');
			getline(f, model, ';');
			getline(f, cena, ';');
			double cena1 = stod(cena);
			getline(f, br, ';');
			int br1 = stoi(br.c_str());
			getline(f, info1, ';');
			int info11 = stoi(info1.c_str());
			getline(f, info2, ';');
			int info22 = stoi(info2.c_str());
			cout << "Komputur:" << endl;
			cout << "\t" << "Marka: " << marka << "\t" << "Model: " << model << "\t" << "Cena: " << cena << "\t" << "Broi: " << br << "\t" << "Razmer: " << info1 << "\t" << "Teglo: " << info2;
			cout << endl << endl;


			Add(new Laptopi(ID, model, cena1, br1, info11, info22));

		}
		if (ID == "TK") {


			getline(f, marka, ';');
			getline(f, model, ';');
			getline(f, cena, ';');
			double cena1 = stod(cena);
			getline(f, br, ';');
			int br1 = stoi(br.c_str());
			getline(f, info1, ';');
			int info11 = stoi(info1.c_str());
			getline(f, info2, ';');
			int info22 = stoi(info2.c_str());
			cout << "Komputur:" << endl;
			cout << "\t" << "Marka: " << marka << "\t" << "Model: " << model << "\t" << "Cena: " << cena << "\t" << "Broi: " << br << "\t" << "3g: " << info1 << "\t" << "gps: " << info2;
			cout << endl << endl;


			Add(new Tableti(ID, model, cena1, br1, info11, info22));

		}
		f.close();

	}

}
int main() {
	queueProducts q;
	string s, s1;
	double suma;
	int br;
	int g;
	int u;
	int in;
	int k;
	int p;
	double t;
	char ch = '9';

	do {
		cout << "\n\nMAIN MENU\n1. Tableti\n2. Laptopi\n3. Komputri\n4. Process\n5.  List\n6. Load File\n0. Exit\n\nChoose: ";
		cin >> ch; cin.ignore(10, '\n');
		if (ch == '0') return 0;
		else if (ch == '1') {

			cout << "Marka: ";getline(cin, s);
			cout << "Model: "; getline(cin, s1);
			cout << "Cena: "; cin >> suma;
			cout << "Broi: "; cin >> br;
			cout << "3G: "; cin >> g;
			cout << "GPS: "; cin >> u;
			q.Add(new Tableti(s, s1, suma, br, g, u));

		}

		else if (ch == '2') {
			cout << "Marka: ";getline(cin, s);
			cout << "Model: "; getline(cin, s1);
			cout << "Cena: "; cin >> suma;
			cout << "Broi: "; cin >> br;
			cout << "Inch: "; cin >> in;
			cout << "Teglo: "; cin >> t;
			q.Add(new Laptopi(s, s1, suma, br, in, t));

		}

		else if (ch == '3') {
			cout << "Marka: ";getline(cin, s);
			cout << "Model: "; getline(cin, s1);
			cout << "Cena: "; cin >> suma;
			cout << "Broi: "; cin >> br;
			cout << "Kutiq: "; cin >> k;
			cout << "Periferiq: "; cin >> p;
			q.Add(new PC(s, s1, suma, br, k, p));

		}
		else if (ch == '4')q.Process();
		else if (ch == '5')q.List();
		else if (ch == '6')q.load_File();
		else if (ch == '7')q.save_File();
	} while (ch != '0');



	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with load/save File fstream

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15434
  • View blog
  • Posts: 61,849
  • Joined: 12-June 08

Re: Problem with load/save File fstream

Posted 07 December 2019 - 10:06 AM

From what I can tell you should be only using 'getline' in the while loop, and then breaking apart the string that returns from 'get line' instead of calling 'getline' a ton of times.
Was This Post Helpful? 0
  • +
  • -

#3 galko441   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 19

Re: Problem with load/save File fstream

Posted 07 December 2019 - 10:34 AM

Hello. Im new with programming . Can u give me an example ?
Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1862
  • View blog
  • Posts: 6,711
  • Joined: 19-February 09

Re: Problem with load/save File fstream

Posted 07 December 2019 - 03:25 PM

Hi, you are probably being confused by the closing of the file stream within the while loop in the function load_File.
So the program runs through the while loop once, then the file stream is closed and getline fails.

281        f.close();
282	 
283	    }
284	 
285	}


Was This Post Helpful? 0
  • +
  • -

#5 galko441   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 19

Re: Problem with load/save File fstream

Posted 08 December 2019 - 01:52 AM

I tried everything but i am stuck in getting all lines from load file and I want to make the information to be saved automatically.
Was This Post Helpful? 0
  • +
  • -

#6 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1862
  • View blog
  • Posts: 6,711
  • Joined: 19-February 09

Re: Problem with load/save File fstream

Posted 08 December 2019 - 10:19 AM

Hi, did you make the change I suggested?

You need to debug, you could use a debugger or print out data from the function to see what is going wrong.

You seem to be reading ID as a number but checking it against a string eg "PC".
Was This Post Helpful? 0
  • +
  • -

#7 galko441   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 19

Re: Problem with load/save File fstream

Posted 08 December 2019 - 10:39 AM

I fixed the code. But when i load the file the last line is printed twice and when i save the file every second line is printed twice.

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;
//=================================================================================================================================================


fstream f;



class Prodajbi
{
public:
	virtual void print_Info() = 0;
	virtual double calc_Tax() = 0;
	virtual void save_Info() = 0;
protected:
	string marka;
	string model;
	double cena;
	int br;
};
//------------------------------------------------------------------------------------------------------------------------------------------------
class PC : public Prodajbi
{
public:
	PC(string ime, string mod, double suma, int broika, int k, int p) {
		marka = ime;
		model = mod;
		cena = suma;
		br = broika;
		kutiq = k;
		per = p;

	}
	virtual void print_Info() {
		cout << "Kategoriq: Komputri " << endl;
		cout << "Marka: " << marka << endl;
		cout << "Model: " << model << endl;
		cout << "Bazova cena: " << cena << endl;
		cout << "Broi: " << br << endl;
		cout << "Tip kutiq: " << kutiq;
		if (kutiq == 1) cout << "\t" << "1 - Mini ATX" << endl;
		if (kutiq == 2) cout << "\t" << "2 - ATX" << endl;
		if (kutiq == 3) cout << "\t" << "3 - XL ATX" << endl;
		cout << "Tip periferiq: " << per;
		if (per == 1) cout << "\t" << "1 - Mishka" << endl;
		if (per == 2) cout << "\t" << "2 - Klaviatura" << endl;
		if (per == 3) cout << "\t" << "3 - Mishka i klaviatura" << endl;
		cout << "Kraina cena: " << calc_Tax() << endl;
	}
	virtual double calc_Tax() {
		double cena1 = cena * br;
		if (kutiq == 1) cena1 += 20;
		if (kutiq == 2) cena1 += 30;
		if (kutiq == 3) cena1 += 40;
		if (per == 1) cena1 += 20;
		if (per == 2) cena1 += 30;
		if (per == 3) cena1 += 50;
		return cena1;
	}
	virtual void save_Info()
	{
		f << "PC" << ";" << "3" << ";" << marka << ";" << model << ";" << cena << ";" << br << ";" << kutiq << ";" << per << ";" << endl;
	}
private:
	double cena;
	int kutiq;
	int per;
};
//-------------------------------------------------------------------------------------------------------------------------------------------------
class Laptopi : public Prodajbi
{
public:
	Laptopi(string ime, string mod, double suma, int broika, int in, double t) {
		marka = ime;
		model = mod;
		cena = suma;
		br = broika;
		inch = in;
		os = t;
	}
	virtual void print_Info() {
		cout << "Kategoriq: Laptopi " << endl;
		cout << "Marka: " << marka << endl;
		cout << "Model: " << model << endl;
		cout << "Bazova cena: " << cena << endl;
		cout << "Broi: " << br << endl;
		cout << "Razmer: " << inch;
		if (inch == 1) cout << "\t" << "1 - 13'" << endl;
		if (inch == 2) cout << "\t" << "2 - 15'" << endl;
		if (inch == 3) cout << "\t" << "3 - 17'" << endl;
		cout << "OS: " << os;
		if (os == 1) cout << "\t" << "1 - Ima" << endl;
		if (os == 2) cout << "\t" << "2 - Nqma" << endl;
		cout << "Kraina cena: " << calc_Tax() << endl;
	}
	virtual double calc_Tax() {
		double cena1 = cena * br;
		if (inch == 1) cena1 += 0;
		if (inch == 2) cena1 += 100;
		if (inch == 3) cena1 += 200;
		if (os == 1) cena1 += 120;
		if (os == 2) cena1 += 0;
		return cena1;
	}
	virtual void save_Info()
	{
		f << "Laptopi" << ";" << "2" << ";" << marka << ";" << model << ";" << cena << ";" << br << ";" << inch << ";" << os << ";" << endl;
	}
private:
	double cena;
	int inch;
	double os;

};
//--------------------------------------------------------------------------------------------------------------------------------------------------
class Tableti : public Prodajbi
{
public:
	Tableti(string ime, string mod, double suma, int broika, int g, int u)
	{
		marka = ime;
		model = mod;
		cena = suma;
		br = broika;
		trig = g;
		gps = u;
	}
	virtual void print_Info() {
		cout << "Kategoriq: Tableti " << endl;
		cout << "Marka: " << marka << endl;
		cout << "Model: " << model << endl;
		cout << "Bazova cena: " << cena << endl;
		cout << "Broi: " << br << endl;
		cout << "3G: " << trig;
		if (trig == 1) cout << "\t" << "1 - Ima" << endl;
		if (trig == 2) cout << "\t" << "2 - Nqma" << endl;
		cout << "GPS: " << gps;
		if (gps == 1) cout << "\t" << "1 - Ima" << endl;
		if (gps == 2) cout << "\t" << "2 - Nqma" << endl;
		cout << endl;
		cout << "Kraina cena: " << calc_Tax() << endl;
	}
	virtual double calc_Tax() {
		double cena1 = cena * br;
		if (trig == 1) cena1 += 40;
		if (trig == 2) cena1 += 0;
		if (gps == 1) cena1 += 60;
		if (gps == 2) cena1 += 0;
		return cena1;
	}
	virtual void save_Info()
	{
		f << "Tableti" << ";" << "1" << ";" << marka << ";" << model << ";" << cena << ";" << br << ";" << trig << ";" << gps << ";" << endl;
	}
private:
	double cena;
	int trig;
	int gps;
};
//---------------------------------------------------------------------------------------------------------------------------------------------------
class queueProducts
{
public:
	queueProducts() {};
	void save_File();
	void load_File();
	void Add(Prodajbi* obj) {
		opashka.push_back(obj);
	}
	void Process() {
		if (opashka.empty()) { //.size==0
			return;
		}
		cout << "Obrabotka na poruchkata: " << endl; opashka[0]->print_Info();cout << endl;
		cout << "Duljimata suma ot klienta e " << opashka[0]->calc_Tax() << endl;
		opashka.erase(opashka.begin());

	}
	void List() {
		cout << "Neobraboteni poruchki:" << endl << endl;
		for (size_t i = 0; i < opashka.size(); i++) {
			opashka[i]->print_Info();
		}
		cout << endl << "Broqt na neobrabotenite ot klienta poruchki e " << opashka.size() << endl;
	}
private:
	vector<Prodajbi*> opashka;

};

void queueProducts::save_File() {
	f.open("data.txt", fstream::out | std::fstream::app);
	for (size_t x = 0; x < opashka.size(); x++) {
		opashka[x]->save_Info();
	}
	f.close();
}
void queueProducts::load_File() {
	f.open("data.txt", fstream::in);
	if (!f.is_open()) return;
	string ID1, ID, marka, model, cena, br, info1, info2;
	while (!f.eof())
	{
		getline(f, ID1, ';');
		getline(f, ID, ';');
		getline(f, marka, ';');
		getline(f, model, ';');
		getline(f, cena, ';');
		getline(f, br, ';');
		getline(f, info1, ';');
		getline(f, info2, ';');
		if (ID.empty()) continue;
		if (ID == "1")
		{
			cout << "Tableti:" << endl;
			cout << "ID: " << ID << ";" << "Marka: " << marka << ";" << "Model: " << model << ";" << "Cena: " << cena << ";" << "Broi: " << br << ";" << "Tip kutiq: " << info1 << ";" << "Periferiq: " << info2;
			cout << endl << endl;
		}
		if (ID == "2")
		{
			cout << "Laptopi:" << endl;
			cout << "ID: " << ID << ";" << "Marka: " << marka << ";" << "Model: " << model << ";" << "Cena: " << cena << ";" << "Broi: " << br << ";" << "Razmer: " << info1 << ";" << "os: " << info2;
			cout << endl << endl;
		}
		if (ID == "3")
		{
			cout << "Komputur:" << endl;
			cout << "ID: " << ID << ";" << "Marka: " << marka << ";" << "Model: " << model << ";" << "Cena: " << cena << ";" << "Broi: " << br << ";" << "3G: " << info1 << ";" << "GPS: " << info2;
			cout << endl << endl;
		}
		if (f.eof()) break;
	}

	f.close();


}
int main() {

	queueProducts q;
	string s, s1;
	double suma;
	int br;
	int g;
	int u;
	int in;
	int k;
	int p;
	double t;
	char ch = '9';


	do {
		cout << "\n\nMAIN MENU\n1. Tableti\n2. Laptopi\n3. Komputri\n4. Obrabotka\n5. List\n6.Vsichko \n0. Exit\n\nChoose: ";
		cin >> ch; cin.ignore(10, '\n');
		if (ch == '0') return 0;
		else if (ch == '1') {

			cout << "Marka: ";getline(cin, s);
			cout << "Model: "; getline(cin, s1);
			cout << "Cena: "; cin >> suma;
			cout << "Broi: "; cin >> br;
			cout << "3G: "; cin >> g;
			cout << "GPS: "; cin >> u;
			q.Add(new Tableti(s, s1, suma, br, g, u));
			q.save_File();

		}

		else if (ch == '2') {
			cout << "Marka: ";getline(cin, s);
			cout << "Model: "; getline(cin, s1);
			cout << "Cena: "; cin >> suma;
			cout << "Broi: "; cin >> br;
			cout << "Inch: "; cin >> in;
			cout << "os: "; cin >> t;
			q.Add(new Laptopi(s, s1, suma, br, in, t));
			q.save_File();

		}

		else if (ch == '3') {
			cout << "Marka: ";getline(cin, s);
			cout << "Model: "; getline(cin, s1);
			cout << "Cena: "; cin >> suma;
			cout << "Broi: "; cin >> br;
			cout << "Kutiq: "; cin >> k;
			cout << "Periferiq: "; cin >> p;
			q.Add(new PC(s, s1, suma, br, k, p));
			q.save_File();
		}
		else if (ch == '4')q.Process();
		else if (ch == '5')q.List();
		else if (ch == '6')q.load_File();

	} while (ch != '0');
}

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7192
  • View blog
  • Posts: 24,371
  • Joined: 05-May 12

Re: Problem with load/save File fstream

Posted 08 December 2019 - 11:47 AM

How is the code in post #7 supposed to work with your "data.txt" from post #1? The load_file() seems to be expecting 8 fields of data, but your "data.txt" from post #1 is only looks to have 5 fields.

The way you state your current issues, you say that you have problems with loading first, and then say that you have problems with saving second. Given that, we can only assume that you are trying to pick option "6" first which is to load a file, but as I mentioned earlier, your "data.txt" doesn't look compatible with your code.

If your expectation was for us to run your code and do a save prior to loading, then you'll need to give us the exact steps to reproduce your problem. What menu choices are you picking? What inputs are you giving?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is offline

  • member icon

Reputation: 5783
  • View blog
  • Posts: 17,685
  • Joined: 25-December 09

Re: Problem with load/save File fstream

Posted 08 December 2019 - 12:00 PM

Also realize that using eof() to control a data entry is not the best idea for a couple of reasons. First eof() is just one possibility of a failure, second it usually leads to the apparent duplication of the last record (caused by improper timing of the test). You should consider using the actual read to control the loop instead.

You should also get rid of that horrible global variable and use local instances instead.

Lastly for now what is with all the "virtual" functions in the children?


Jim
Was This Post Helpful? 0
  • +
  • -

#10 galko441   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-December 19

Re: Problem with load/save File fstream

Posted 08 December 2019 - 12:35 PM

Well , this program is for selling computers , in the main menu i got tablets,laptops,computers,proccess order,waiting orders and all orders(load_file) put information for brand,model,price and etc.I tried to save files with ID thats why i got more getlines in load_File(). Now save_file works but when i add Laptops and then i try to add Computers the final result is:

For example:
Adding computers / main menu 3/
Brand:Dell
Model: AS515-T
Price: 1500
Count: 1
Box: 1(Yes)
Accessories: 1 (Mouse)

and adding laptops in the file i got 2 same lines with computer and one line with laptop and another one with laptop(From load_File) .


Computers:
ID: 3;brand: Dell;Model: AS515-T;price: 1500;Count: 1;3G: 1;GPS: 1

Computers:
ID: 3;brand: Dell;Model: AS515-T;price: 1500;Count: 1;3G: 1;GPS: 1

Laptops:
ID: 2;brand: Laptop1;Model: Laptop1;price: 1;Count: 1;Size: 1;os: 1

Laptops:
ID: 2;brand: Laptop1;Model: Laptop1;price: 1;Count: 1;Size: 1;os: 1


When i add vec.erase(vec.begin()); in queueProducts::save_File() everything is OK but the List and Proccess from main menu doesnt work because of erase.

Here is the code in English.
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;
//=================================================================================================================================================
fstream f;
class Sales
{
public:
	virtual void print_Info() = 0;
	virtual double calc_Tax() = 0;
	virtual void save_Info() = 0;
protected:
	string brand;
	string model;
	double price;
	int br;
};
//------------------------------------------------------------------------------------------------------------------------------------------------
class PC : public Sales
{
public:
	PC(string name, string mod, double suma, int count, int k, int p) {
		brand = name;
		model = mod;
		price = suma;
		br = count;
		box = k;
		per = p;

	}
	virtual void print_Info() {
		cout << "Category: Computers " << endl;
		cout << "brand: " << brand << endl;
		cout << "Model: " << model << endl;
		cout << "Base price: " << price << endl;
		cout << "Count: " << br << endl;
		cout << "Box type: " << box;
		if (box == 1) cout << "\t" << "1 - Mini ATX" << endl;
		if (box == 2) cout << "\t" << "2 - ATX" << endl;
		if (box == 3) cout << "\t" << "3 - XL ATX" << endl;
		cout << "Type accessories: " << per;
		if (per == 1) cout << "\t" << "1 - Mouse" << endl;
		if (per == 2) cout << "\t" << "2 - Keyboard" << endl;
		if (per == 3) cout << "\t" << "3 - Mouse and Keyboard" << endl;
		cout << "Final price: " << calc_Tax() << endl;
	}
	virtual double calc_Tax() {
		double price1 = price * br;
		if (box == 1) price1 += 20;
		if (box == 2) price1 += 30;
		if (box == 3) price1 += 40;
		if (per == 1) price1 += 20;
		if (per == 2) price1 += 30;
		if (per == 3) price1 += 50;
		return price1;
	}
	virtual void save_Info()
	{
		f << "PC" << ";" << "3" << ";" << brand << ";" << model << ";" << price << ";" << br << ";" << box << ";" << per << ";" << endl;
	}
private:
	double price;
	int box;
	int per;
};
//-------------------------------------------------------------------------------------------------------------------------------------------------
class Laptops : public Sales
{
public:
	Laptops(string name, string mod, double suma, int count, int in, double t) {
		brand = name;
		model = mod;
		price = suma;
		br = count;
		inch = in;
		os = t;
	}
	virtual void print_Info() {
		cout << "Category: Laptops " << endl;
		cout << "brand: " << brand << endl;
		cout << "Model: " << model << endl;
		cout << "Base price: " << price << endl;
		cout << "Count: " << br << endl;
		cout << "Size: " << inch;
		if (inch == 1) cout << "\t" << "1 - 13'" << endl;
		if (inch == 2) cout << "\t" << "2 - 15'" << endl;
		if (inch == 3) cout << "\t" << "3 - 17'" << endl;
		cout << "OS: " << os;
		if (os == 1) cout << "\t" << "1 - Yes" << endl;
		if (os == 2) cout << "\t" << "2 - No" << endl;
		cout << "Final price: " << calc_Tax() << endl;
	}
	virtual double calc_Tax() {
		double price1 = price * br;
		if (inch == 1) price1 += 0;
		if (inch == 2) price1 += 100;
		if (inch == 3) price1 += 200;
		if (os == 1) price1 += 120;
		if (os == 2) price1 += 0;
		return price1;
	}
	virtual void save_Info()
	{
		f << "Laptops" << ";" << "2" << ";" << brand << ";" << model << ";" << price << ";" << br << ";" << inch << ";" << os << ";" << endl;
	}
private:
	double price;
	int inch;
	double os;

};
//--------------------------------------------------------------------------------------------------------------------------------------------------
class Tablets : public Sales
{
public:
	Tablets(string name, string mod, double suma, int count, int g, int u)
	{
		brand = name;
		model = mod;
		price = suma;
		br = count;
		trig = g;
		gps = u;
	}
	virtual void print_Info() {
		cout << "Category: Tablets " << endl;
		cout << "brand: " << brand << endl;
		cout << "Model: " << model << endl;
		cout << "Base price: " << price << endl;
		cout << "Count: " << br << endl;
		cout << "3G: " << trig;
		if (trig == 1) cout << "\t" << "1 - Yes" << endl;
		if (trig == 2) cout << "\t" << "2 - No" << endl;
		cout << "GPS: " << gps;
		if (gps == 1) cout << "\t" << "1 - Yes" << endl;
		if (gps == 2) cout << "\t" << "2 - No" << endl;
		cout << endl;
		cout << "Final price: " << calc_Tax() << endl;
	}
	virtual double calc_Tax() {
		double price1 = price * br;
		if (trig == 1) price1 += 40;
		if (trig == 2) price1 += 0;
		if (gps == 1) price1 += 60;
		if (gps == 2) price1 += 0;
		return price1;
	}
	virtual void save_Info()
	{
		f << "Tablets" << ";" << "1" << ";" << brand << ";" << model << ";" << price << ";" << br << ";" << trig << ";" << gps << ";" << endl;
	}
private:
	double price;
	int trig;
	int gps;
};
//---------------------------------------------------------------------------------------------------------------------------------------------------
class queueProducts
{
public:
	queueProducts() {};
	void save_File();
	void load_File();
	void Add(Sales* obj) {
		vec.push_back(obj);
	}
	void Process() {
		if (vec.empty()) { //.size==0
			return;
		}
		cout << "Order processing: " << endl; vec[0]->print_Info();cout << endl;
		cout << "Тhe amount due from the customer is " << vec[0]->calc_Tax() << endl;
		vec.erase(vec.begin());

	}
	void List() {
		cout << "Unprocessed orders:" << endl << endl;
		for (size_t i = 0; i < vec.size(); i++) {
			vec[i]->print_Info();
		}
		cout << endl << "The number of orders not processed by the customer is " << vec.size() << endl;
	}
private:
	vector<Sales*> vec;

};

void queueProducts::save_File() {
	f.open("data.txt", fstream::out | std::fstream::app);
	for (size_t x = 0; x < vec.size(); x++) {
		vec[x]->save_Info();
	}
	f.close();
}
void queueProducts::load_File() {
	f.open("data.txt", fstream::in);
	if (!f.is_open()) return;
	string ID1, ID, brand, model, price, br, info1, info2;
	while (!f.eof())
	{
		getline(f, ID1, ';');
		getline(f, ID, ';');
		getline(f, brand, ';');
		getline(f, model, ';');
		getline(f, price, ';');
		getline(f, br, ';');
		getline(f, info1, ';');
		getline(f, info2, ';');
		if (ID.empty()) continue;
		if (ID == "1")
		{
			cout << "Tablets:" << endl;
			cout << "ID: " << ID << ";" << "brand: " << brand << ";" << "Model: " << model << ";" << "price: " << price << ";" << "Count: " << br << ";" << "Box type: " << info1 << ";" << "accessories: " << info2;
			cout << endl << endl;
		}
		if (ID == "2")
		{
			cout << "Laptops:" << endl;
			cout << "ID: " << ID << ";" << "brand: " << brand << ";" << "Model: " << model << ";" << "price: " << price << ";" << "Count: " << br << ";" << "Size: " << info1 << ";" << "os: " << info2;
			cout << endl << endl;
		}
		if (ID == "3")
		{
			cout << "Computers:" << endl;
			cout << "ID: " << ID << ";" << "brand: " << brand << ";" << "Model: " << model << ";" << "price: " << price << ";" << "Count: " << br << ";" << "3G: " << info1 << ";" << "GPS: " << info2;
			cout << endl << endl;
		}
		if (f.eof()) break;
	}

	f.close();


}
int main() {

	queueProducts q;
	string s, s1;
	double suma;
	int br;
	int g;
	int u;
	int in;
	int k;
	int p;
	double t;
	char ch = '9';


	do {
		cout << "\n\nMAIN MENU\n1. Tablets\n2. Laptops\n3. Computers\n4. Processing\n5. List\n6.All orders \n0. Exit\n\nChoose: ";
		cin >> ch; cin.ignore(10, '\n');
		if (ch == '0') return 0;
		else if (ch == '1') {

			cout << "brand: ";getline(cin, s);
			cout << "Model: "; getline(cin, s1);
			cout << "price: "; cin >> suma;
			cout << "Count: "; cin >> br;
			cout << "3G: "; cin >> g;
			cout << "GPS: "; cin >> u;
			q.Add(new Tablets(s, s1, suma, br, g, u));
			q.save_File();

		}

		else if (ch == '2') {
			cout << "brand: ";getline(cin, s);
			cout << "Model: "; getline(cin, s1);
			cout << "price: "; cin >> suma;
			cout << "Count: "; cin >> br;
			cout << "Inch: "; cin >> in;
			cout << "os: "; cin >> t;
			q.Add(new Laptops(s, s1, suma, br, in, t));
			q.save_File();

		}

		else if (ch == '3') {
			cout << "brand: ";getline(cin, s);
			cout << "Model: "; getline(cin, s1);
			cout << "price: "; cin >> suma;
			cout << "Count: "; cin >> br;
			cout << "box: "; cin >> k;
			cout << "accessories: "; cin >> p;
			q.Add(new PC(s, s1, suma, br, k, p));
			q.save_File();
		}
		else if (ch == '4')q.Process();
		else if (ch == '5')q.List();
		else if (ch == '6')q.load_File();

	} while (ch != '0');
}

Was This Post Helpful? 0
  • +
  • -

#11 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1862
  • View blog
  • Posts: 6,711
  • Joined: 19-February 09

Re: Problem with load/save File fstream

Posted 08 December 2019 - 01:21 PM

You are appending to the file

192	    f.open("data.txt", fstream::out | std::fstream::app);


so that will add data to the file, it might be the cause of duplicate lines.

Do you need to save the file immediately after you added an entry?

You could load the file at the beginning of the program and automatically save at the end of the program.

You could have a save file menu option.

You should also be deleting the objects created by new at the end of the program probably with a destructor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1