Hi Im really stuck on one problem. I trying to save some data to a file.
im using Borland compiler.
the challenege is to create a new product and save its details to a text file.
1. the user inputs product details i.e. Barcode, Name, Price, and quantity via the interface
2. the user clicks on save button, and it creates a file to which the information is saved

heres what ive got so far:

Product.h file

class Product
{
	private: 
			AnsiString BarCodeField[];
			AnsiString ProductNameField[];
			double PriceExVAT;
			double PriceIncVAT;
			int CurrentSupplyField;
			int MinimumSupplyField;

	public:
			void CreateNewProduct(AnsiString theBarCodeField[],
								  AnsiString theProductNameField[],
								  double thePriceExVAT,
								  double thePriceIncVAT,
								  int theCurrentSupplyField,
								  int theMinimumSupplyField);


			AnsiString getBarCode();
			AnsiString getProductName();
			double getExVAT();
			double getIncVAT();
			int getCurrentSupply();
			int getMinimumSupply();

			Product();
			~Product();

};

Product.cpp file

Product::Product()
{

}

Product::~Product()
{

}

void Product::CreateNewProduct(AnsiString theBarCodeField[],
								AnsiString theProductNameField[],
								double thePriceExVAT,
								double thePriceIncVAT,
								int theCurrentSupplyField,
								int theMinimumSupplyField)
								{
									BarCodeField[] = theBarCodeField[];
									ProductNameField[] = theProductNameField[];
									PriceExVAT = thePriceExVAT;
									PriceIncVAT = thePriceIncVAT;
									CurrentSupplyField = theCurrentSupplyField;
								}
//---------------------------------------------------------------------------
AnsiString Product::getBarCode()
{
	return BarCodeField[];
}
//---------------------------------------------------------------------------
AnsiString Product::getProductName()
{
	return ProductNameField[];
}
//---------------------------------------------------------------------------
double Product::getExVAT()
{
	return PriceExVAT;
}
//---------------------------------------------------------------------------
double Product::getIncVAT()
{
	return PriceIncVAT;
}
//---------------------------------------------------------------------------
int Product::getCurrentSupply()
{
	return CurrentSupplyField;
}
//---------------------------------------------------------------------------

int Product::getMinimumSupply()
{
	return MinimumSupplyField;
}
//---------------------------------------------------------------------------

AddProductForm.cpp file

void __fastcall TForm2::AddToStockClick(TObject *Sender)
{
	FILE * FSave;

	theProduct.CreateNewProduct(//???????? HELP PLEASE!!!!!!! WHAT PARAMETERS GO HERE???
								//BarcodeBox,
								//ProductName, 
								//ExVAT,
								//IncVAT
								//CurrentSupply->Text,
								//MinimumSupply->Text
								); ???
	{
		strcpy(theProduct.getBarCode, BarcodeBox->Text.c_str());
                //i want to copy the data from a TEdit box into the getBarCode variable and save it to file
	}

if(SaveDialog1->Execute())
	{
		FSave = fopen(SaveDialog1->FileName.c_str(),"w");
		ProdConfirm->Visible = true;

		if(FSave == NULL)
		{
			MessageBox(NULL,"The file could not be saved","Error", MB_OK);
			return;
		}

		fprintf(FSave, "%s\t", BarCodeField);
		fprintf(FSave, "%s\t", ProductNameField);
		fprintf(FSave, "%.2f\t", PriceExVAT);
		fprintf(FSave, "%.2f\t", PriceIncVAT);
		fprintf(FSave, "%s\t", CurrentSupplyField);
		fprintf(FSave, "%s\t", MinimumSupplyField);
	}
	fclose(FSave);

Recommended Answers

All 5 Replies

What you should have is make the Product class composed of an another
class. The another class should hold all the data thats needed, like
name or barcode.

Then you can use that as a parameter to in createNewProduct.

Is your createNewProduct creating a temporary(initialized to the
parameters passed) variable an inserting it into a .txt file?

Im not quite sure what you mean. Can you use an example? This is the first time im having a go at Object-Oriented programing

the idea of CreateNewProduct is to get the input from the user (submitd via TEdit boxes) via the user interface, and pass it as a whole to a text file on a single line. Im open to suggestions and ideas

This is what I mean :

struct Item {
   string Id;
   string Name;
   string barCode;
  //more stuff
};

class Product
{
   Item productInformation;
 //stuff
};

or you can make the Item struct a public inner class.

From there you can have this method in your product class :

bool createNewObject( Item item, string fileName)
{
    std::ostream insertToFile( fileName.c_str() );
   if( ! insertToFile ) return false;
    insertToFile << "Id : " << item.id  << endl;
    insertToFile << "Name : " << item.name << endl;
    insertToFile << "Barcode: " << item.barCode << endl;
 
   return true;
}

Is this what you were going for?

Sorry to be a pain, but i dont know how that works. How are you getting the input from the user?

Its not. If you wan't to get the users input then get it first, and then
call the createNewObject with the input passed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.