I asked this question yesterday

http://stackoverflow.com/questions/20908568/calculating-daily-and-monthly-sales-from-text-file

and now I have come up with this code:

stock.h

#ifndef stock_stock_h
#define stock_stock_h
#include <iostream>

class  stock  {

public:
    stock() {
        itemName = " ";
        unitPrice = " ";
        quantityPurchased = " ";
        day = " ";
        month = " ";
        year = " ";
    }

    stock(std::string itemName,std::string unitPrice,std::string quantityPurchased,
          std::string day,std::string month,std::string year);

    std::string getItemName();
    std::string getUnitPrice();
    std::string getQuantityPurchased();
    std::string getDateDay();
    std::string getDateMonth();
    std::string getDateYear();


    void setItemDescription(std::string itemName);
    void setUnitPrice(std::string unitPrice);
    void setQuantityPurchased(std::string quantityPurchased);
    void setDateDay(std::string day);
    void setDateMonth(std::string month);
    void setDateYear(std::string year);


private:
    std::string itemName,unitPrice,quantityPurchased,day,month,year;
};
#endif

main.cpp

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

using namespace std;

stock::stock(string itemName,string unitPrice,string quantityPurchased,string day,string    month,string year)  {

    setItemDescription(itemName);
    setUnitPrice(unitPrice);
    setQuantityPurchased(quantityPurchased);
    setDateDay(day);
    setDateMonth(month);
    setDateYear(year);

};

string stock::getItemName()    {
    return itemName;
}
string stock::getUnitPrice()  {
    return unitPrice;
}

string stock::getQuantityPurchased()  {
    return quantityPurchased;
}

string stock::getDateDay()  {
    return day;
}

string stock::getDateMonth()  {
   return month;
}

string stock::getDateYear()  {
   return year;
}

void stock::setItemDescription(std::string itemName) {
    this->itemName = itemName;
}

void stock::setUnitPrice(std::string unitPrice) {
    this->unitPrice = unitPrice;
}

void stock::setQuantityPurchased(std::string quantityPurchased) {
    this->quantityPurchased = quantityPurchased;
}

void stock::setDateDay(std::string day) {
    this->day = day;
}

void stock::setDateMonth(std::string month) {
    this->month = month;
}

void stock::setDateYear(std::string year) {
    this->year = year;
}

int main(){
    vector<stock> itemDetails;
    string line;
    string itemName;
    string unitPrice;
    string quantityPurchased;
    string inputMonth;
    string day;
    string month;
    string year;
    double uPrice;
    int qPurchased;
    double totalIndividualItemSales;
    double totalSalesOfMonthDec = 0.0;
    double totalSalesOfMonthJan = 0.0;
    double totalSalesOfMonthFeb = 0.0;

    //put your text file name inside the ""
    ifstream readFile("/Users/jeremykeh/Desktop/stock.txt");

    while(getline(readFile,line))   {
        stringstream iss(line);
        getline(iss, itemName,':');
        getline(iss, unitPrice, ':');
        getline(iss, quantityPurchased, ':');
        getline(iss, day, '-');
        getline(iss, month, '-');
        getline(iss, year, '-');

        //consturctor

        stock splitedColumns(itemName,
                             unitPrice,
                             quantityPurchased,
                             day,
                             month,
                             year
                             );
        itemDetails.push_back(splitedColumns);

    }
    readFile.close();

    cout << "Info reterived from file" << endl;

    for (int i =0; i<itemDetails.size(); i++) {

        if(itemDetails[i].getDateMonth() == "Dec" && itemDetails[i].getDateYear() == "2013")  {
            istringstream uPbuffer(itemDetails[i].getUnitPrice());
            istringstream qPbuffer(itemDetails[i].getQuantityPurchased());

            uPbuffer >> uPrice;
            qPbuffer >> qPurchased;
            totalIndividualItemSales = uPrice * qPurchased;
            totalSalesOfMonthDec += totalIndividualItemSales;

        }

        if(itemDetails[i].getDateMonth() == "Jan" && itemDetails[i].getDateYear() == "2014")  {
            istringstream uPbuffer(itemDetails[i].getUnitPrice());
            istringstream qPbuffer(itemDetails[i].getQuantityPurchased());

            uPbuffer >> uPrice;
            qPbuffer >> qPurchased;
            totalIndividualItemSales = uPrice * qPurchased;
            totalSalesOfMonthJan += totalIndividualItemSales;

        }

        if(itemDetails[i].getDateMonth() == "Feb" && itemDetails[i].getDateYear() == "2014")  {
            istringstream uPbuffer(itemDetails[i].getUnitPrice());
            istringstream qPbuffer(itemDetails[i].getQuantityPurchased());

            uPbuffer >> unitPrice;
            qPbuffer >> quantityPurchased;
            totalIndividualItemSales = uPrice * qPurchased;
            totalSalesOfMonthFeb += totalIndividualItemSales;

        }

    }
    cout << "Report Summary of sales(Monthly)" << endl;
    cout << setw(10) << left << "Month" <<setw(5) << "Sales" <<endl;
    cout <<setw(10) << left << "Dec 13" <<setw(5) << totalSalesOfMonthDec <<endl;
    cout <<setw(10) << left << "Jan 14" <<setw(5) << totalSalesOfMonthJan <<endl;
    cout <<setw(10) << left << "Feb 14" <<setw(5) << totalSalesOfMonthFeb <<endl; 
}

Although I've achieved the results I wanted, I feel that I am hard-coding it badly to achieve my results. I wish to know/learn how I can further improve my code from you all.

Thanks
My output

Info reterived from file
Report Summary of sales(Monthly)
Month     Sales
Dec 13    16   
Jan 14    46   
Feb 14    25   

What? No. This is not how OOP works. Why are you defining your class implementation in main.cpp? This should be inside of the .cpp associated with your class file!

Why do you have a empty/initial constructor that initialises some strings? Look at your code, get it to work as just one main file before moving onto more advanced concepts as OO.

Alright. I guess I messed it up. I will work on it. thanks for advice

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.