I am trying to read a textfile containing some informations in this format,

itemId:itemDescription:itemCategory:itemSubCategory:amountPerUnit:quantity:date.

item.txt

1:macbook:electronics:laptop:100:100:18-Oct-13
2:Iphone 5:electronics:handphone:50:50:18-Oct-13

I am able to extract everything except the itemId. Please advise or suggest what I should change/do to get the id. Thanks in advance.

my output

Id:      <----blank!
Item Description: macbook
item Category: electronics
item Sub Category: laptop
Item Amount Per unit: 100
Item quantity: 100
date: 18-Oct-13
Id:       <----blank!
Item Description: Iphone 5
item Category: electronics
item Sub Category: handphone
Item Amount Per unit: 50
Item quantity: 50
date: 18-Oct-13

expected output

Id: 1
Item Description: macbook
item Category: electronics
item Sub Category: laptop
Item Amount Per unit: 100
Item quantity: 100
date: 18-Oct-13
Id: 2
Item Description: Iphone 5
item Category: electronics
item Sub Category: handphone
Item Amount Per unit: 50
Item quantity: 50
date: 18-Oct-13

Below is my codes.

manageStock.h

 #ifndef stock_manageStock_h
 #define stock_manageStock_h

manageStock() {

     public: 

       manageStock() {

        itemIdInFile = " ";
        itemDescriptionInFile = " ";
        itemCategoryInFile = " ";
        itemSubCategoryInFile = " ";
        amountPerUnitInFile = " ";
        quantityInFile = " ";
        dateInFile = " ";
    }

        manageStock(std::string itemIdInFile,std::string itemDescriptionInFile,std::string itemCategoryInFile,std::string itemSubCategory,std::string amountPerUnitInFile, 
                    std::string quantityInFile,std::string dateInFile);
       void editItem(); 
    private
      std::string itemDescriptionInFile,itemCategoryInFile,itemSubCategoryInFile,amountPerUnitInFile,quantityInFile,dateInFile;



#endif

manageStock.cpp

#include "manageStock.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream> 
#include <iterator>
#include <cassert>
#include <vector>

manageStock::manageStock(string itemIdInFile,string itemDescriptionInFile,string itemCategoryInFile,string itemSubCategoryInFile ,string amountPerUnitInFile, string quantityInFile,string dateInFile)   {
    setItemId(itemIdInFile);
    setItemDescription(itemDescriptionInFile);
    setItemCategory(itemCategoryInFile);
    setItemSubCategory(itemSubCategoryInFile);
    setAmountPerUnit(amountPerUnitInFile);
    setQuantity(quantityInFile);
    setDate(dateInFile);
};

string manageStock::getItemId()    {
    return itemIdInFile;
}
string manageStock::getItemDescription()  {
    return itemDescriptionInFile;
}
string manageStock::getItemCategory()   {
    return itemCategoryInFile;
}
string manageStock::getItemSubCategory()   {
    return itemSubCategoryInFile;
}
string manageStock::getAmountPerUnit()   {
    return amountPerUnitInFile;
}

string manageStock::getQuantity()   {
    return quantityInFile;
}
string manageStock::getDate()   {
    return dateInFile;
}

void manageStock::setItemId(string itemIdinFile)    {
    this->itemIdInFile = itemIdInFile;
}
void manageStock::setItemDescription(std::string itemDescriptionInFile) {
    this->itemDescriptionInFile = itemDescriptionInFile;
}

void manageStock::setItemCategory(std::string itemCategoryInFile)  {
    this-> itemCategoryInFile = itemCategoryInFile;
}

void manageStock::setItemSubCategory(std::string itemSubCategoryInFile) {
    this->itemSubCategoryInFile = itemSubCategoryInFile;
}
void manageStock::setAmountPerUnit(std::string amountPerUnitInFile) {
    this->amountPerUnitInFile = amountPerUnitInFile;
}

void manageStock::setQuantity(std::string quantityInFile) {
    this->quantityInFile = quantityInFile;
}

void manageStock::setDate(std::string dateInFile) {
    this->dateInFile = dateInFile;
}

 void manageStock::editItem() {

    vector<manageStock> itemDetails;
    //add this to fill up the position 0 of the vector
    manageStock dummyCons(" "," "," "," "," "," "," ");
    itemDetails.push_back(dummyCons);
    ifstream readFile("item.txt");
    while(getline(readFile,line))   {   

    stringstream iss(line);
    getline(iss, itemIdInFile,':');
    getline(iss, itemDescriptionInFile, ':');
    getline(iss, itemCategoryInFile, ':');
    getline(iss, itemSubCategoryInFile, ':');    
    getline(iss, amountPerUnitInFile, ':'); 
    getline(iss, quantityInFile, ':');
    getline(iss, dateInFile, ':');

    //consturctor
    manageStock itemCons(itemIdInFile,itemDescriptionInFile,itemCategoryInFile,itemSubCategoryInFile,amountPerUnitInFile,quantityInFile,dateInFile);
    itemDetails.push_back(itemCons);

    }
    readFile.close();

     // display data in the vector
    for (int i =1; i<itemDetails.size(); i++) {
        cout <<"Id: " << itemDetails[i].getItemId() << std::endl;
        cout <<"Item Description: " << itemDetails[i].getItemDescription() << std::endl;
        cout <<"item Category: " << itemDetails[i].getItemCategory() << std::endl;
        cout << "item Sub Category: " << itemDetails[i].getItemSubCategory() << std::endl;
        cout << "Item Amount Per unit: " << itemDetails[i].getAmountPerUnit() << std::endl;
        cout << "Item quantity: " << itemDetails[i].getQuantity() << std::endl;
        cout << "date: " << itemDetails[i].getDate() << std::endl;

    }
}

Edited 3 Years Ago by roidbeginnerlvl

itemIdInFile is not declared anywhere in the code you have supplied.

Most of your member functions are not declared in the header you supplied.

I surmise that you have not posted your actual code because the code you have posted would not compile and you imply that you can compile it.

Try this style.
Also you can read the value into an array or just cout. Its even more easier once you know that value format remains the same.

using namespaces std;
string line = "foo: bar: boo:tuna";
string del = ":";

size_t pos = 0;
string token;
while ((pos = line.find(del)) != string::npos) {
    token = st.substr(0, pos);
    cout << token << endl;
    st.erase(0, pos + del.length());
}
cout << st << std::endl;

foo
bar
boo
tuna

Edited 3 Years Ago by richieking: edited

This article has been dead for over six months. Start a new discussion instead.