Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I asked this question this question yesterday, and now I have come up with this code:

I asked this question yesterday, and now I have come up with this code:

I asked this question yesterday, and now I have come up with this code:

added 51 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Parsing store sales content from a text file content

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

Parsing text file content

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

Parsing store sales content from a text file

Info reterived from file
Report Summary of sales(Monthly)
Month Sales
Dec 13 16 
Jan 14 46 
Feb 14 25
Tweeted twitter.com/#!/StackCodeReview/status/419555276233408512
added 7 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

c++-prasing Parsing text file content

I asked this questionthis question yesterday, and now I have come up with this code:

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

and now I have came up with my codes.

stock.h

main.cppmain.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 II've achieved the results I wanted, but I feltfeel that I am hard coding-coding it badly to achieve my results.so I I wish to know/learn how can I can further improve on my codescode from you guysall. thanks

my outputMy output

c++-prasing text file content

I asked this question yesterday

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

and now I have came up with my codes.

stock.h

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 achieved the results I wanted, but I felt that I am hard coding it badly to achieve my results.so I wish to know/learn how can I further improve on my codes from you guys. thanks

my output

Parsing text file content

I asked this question yesterday, and now I have come up with this code:

stock.h

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.

My output

Source Link
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /