First let me tell you what my program is doing. its basically a shopping list program. Whereby the user will input the values and it is then inserted into a text file. There will be 3 inputs (Item Description,Unit Price,Quantity Purchased) and is stored into the text file as (Shoes:$200:2) but when i try to call out the string it is shown as

Item 1
Item Description: Shoes:$200:2
Unit Price:
Quantity Purchased:

I know i need to use the getline() and use a delimiter but i dont know how to put it in my code.

int txtTArrayTrans (fstream& infile, char filename[], StoreTransDetail trans[])
{
	infile.open (filename, ios::in);

	if (!infile.good())
	{
		cout << filename << " open for reading failed" << endl;
		exit(1);
	}
	else
	{
	}

	char p[MAXSTR];
	int count = 0;
	int pos = 1;

	while (!infile.eof())
	{
		while (infile >> p)
		{
			if (pos == 1)
			{
				strcpy(trans[count].item,p);
			}

			if (pos == 2)
			{
				trans[count].unitPrice = p[0] - '0';
			}

			if (pos == 3)
			{
				trans[count].quantity = p[0] - '0';

			}
			pos += 1;
		}
		infile.close ();
		return count;
	}
}

void editTrans(StoreTransDetail trans[])
{
	int transSize;
	fstream infile;
	fstream outfile;
	int editItem;

	char item[MAX];
	int unitPrice;
	int quantity;

	transSize = txtTArrayTrans (infile, "StoreTransDetail.txt", trans);
	infile.close();

	for (int i=0; i<transSize; i++)
	{
		cout << "Item " << i+1 << ")" << endl;
		cout << "Item Description   :\t" << trans[i].item << endl;
		cout << "Unit Price         :\t" << trans[i].unitPrice << endl;
		cout << "Quantity Purchased :\t" << trans[i].quantity << endl;
	}

	cout << "Item to be edit: ";
	cin >> editItem;

	cout << "Item Description: ";
	cin >> item;

	cout << "Unit Price: ";
	cin >> unitPrice;

	cout << "Quantity Pruchased: ";
	cin >> quantity;

	editItem--;

	strcpy(trans[editItem].item,item);
	trans[editItem].unitPrice = unitPrice;
	trans[editItem].quantity = quantity;

	ArrayTtxtTrans (outfile, "StoreTransDetail.txt", trans, transSize);
	outfile.close();
}

First let me tell you what my program is doing. its basically a shopping list program. Whereby the user will input the values and it is then inserted into a text file. There will be 3 inputs (Item Description,Unit Price,Quantity Purchased) and is stored into the text file as (Shoes:$200:2)...

getline reads a line from the file. What do you have in one line of the file? That's a big clue as to why the line in the file is displayed as the item description.

I know i need to use the getline() and use a delimiter but i dont know how to put it in my code.

Try Googling for getline or reading your book about it.

Much simpler :

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>

class Item  {
   std::string Name;
   int Qty;
   int Price;
public:
   Item (std::string Name_,int Qty_,int Price_) :
      Name(Name_),Qty(Qty_),Price(Price_)  {}
   Item () {}
   ~Item () {}
   friend std::istream& operator >> (std::istream& i,Item& it)  {
      return i >> it.Name >> it.Qty >> it.Price;
    }

   friend std::ostream& operator << (std::ostream& o,const Item& it)  {
      return o << it.Name << "\n" << it.Qty << "\n" << it.Price << "\n";
    }

   void Show ()  {
      std::cout << Name << "\n" << Qty << "\n" << Price << "\n";
    }
 };

int main ()  {
   std::ofstream o("file.txt");
   Item asd("asd",12,14);
   o << asd;
   o.close();

   std::ifstream f("file.txt");
   Item t;
   f >> t;
   t.Show();
 }
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.