Relatively new to C++ , the following is my code:
void displaydailyreport()
{
std::string myline;
string date[100]; // array for dates
int dailyprice =0;
ifstream myfile("stockdatabase.txt"); // open textfile
int i,j;
for(i=0;std::getline(myfile,myline);i++) // looping thru the number of lines in the textfile
{
date[i] = stockpile[i].datepurchased; // stockpile.datepurchased give the date,already seperated by :
cout<<date[i]<<endl; // prints out date
for(j=i+1;std::getline(myfile,myline);j++)
{
if(date[i] == date[j]) // REFER TO //PROBLEM// BELOW
dailyprice += stockpile[i].unitprice; // trying to add the total price of the same dates and print the
// datepurchased(datepurchased should be the same) of the total price
// means the total price purchased for 9oct16
}
}
cout<<endl;
}
everything is already retrieved and and seperated by : from the methods i have wrote
stockpile[i].unitprice
will print out the price
stockpile[i].itemdesc
will print out the item description
PROBLEM
I am trying to sum up unitprice of the same dates. and display the total unitprice + date as u can see my textfile ,
if i do the above if statement of date[i] == date[j]
but it won't work because what if there is another 9oct somewhere else?
My textfile is:
itemid:itemdesc:unitprice:datepurchased
22:blueberries:3:9oct16
11:okok:8:9oct16
16:melon:9:10sep16
44:po:9:9oct16
63:juicy:11:11oct16
67:milk:123:12oct16
68:pineapple:43:10oct16
69:oranges:32:9oct16 <--
Does C++ have array object where i can do this :
testArray['9oct16']
//EDIT// after trying Ayak973's answer , compiled with g++ -std=c++11 Main.cpp
Main.cpp: In function ‘void displaydailyreport()’:
Main.cpp:980:26: error: ‘struct std::__detail::_Node_iterator<std::pair<const std::basic_string<char>, int>, false, true>’ has no member named ‘second’
mapIterator.second += stockpile[i].unitprice;