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();
}

Dani AI

Generated

Your file lines like

Shoes:$200:2

are a single token to the extraction operator (operator>>), so your code reads the whole thing as the "item". As hinted, read one full line at a time and then split on the colon. 's suggestion (putting each field on its own line) is a valid alternative, but if the file format is colon‑separated you should parse the line by delimiter.

A robust approach: use std::getline(infile, line) to get each record, then use a std::stringstream and std::getline(ss, field, ':') to extract item, price, and quantity. Strip any leading $ before converting with std::stoi. Avoid while (!infile.eof()) and do not close() the file or return from inside the read loop — that causes immediate termination after the first read.

Example parser (adapt to your StoreTransDetail and calling code):

#include <fstream>
#include <sstream>
#include <string>
#include <vector>

struct StoreTransDetail {
    std::string item;
    int unitPrice;
    int quantity;
};

std::vector<StoreTransDetail> readTransFile(const std::string& filename) {
    std::ifstream in(filename);
    std::vector<StoreTransDetail> result;
    std::string line;
    while (std::getline(in, line)) {
        if (line.empty()) continue;
        std::stringstream ss(line);
        std::string item, priceStr, qtyStr;
        if (!std::getline(ss, item, ':')) continue;
        if (!std::getline(ss, priceStr, ':')) continue;
        if (!std::getline(ss, qtyStr)) continue;
        if (!priceStr.empty() && priceStr.front() == '$') priceStr.erase(0, 1);
        try {
            int price = std::stoi(priceStr);
            int qty = std::stoi(qtyStr);
            result.push_back({item, price, qty});
        } catch (...) {
            // handle malformed line
        }
    }
    return result;
}

Extra tips: prefer std::string over raw C arrays and strcpy; trim whitespace if users may add spaces; handle CR/LF if files come from Windows; and validate std::stoi exceptions. For getline details see std::getline and for conversions see .

Recommended Answers

All 2 Replies

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.