【发布时间】:2016-10-18 08:34:03
【问题描述】:
对 C++ 比较陌生,以下是我的代码:
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;
}
一切都已经被检索和分隔:从我写的方法中
stockpile[i].unitprice会打印出价格
stockpile[i].itemdesc会打印出物品描述
问题
我试图总结相同日期的单价。并显示总单价+日期 你可以看到我的文本文件,
如果我执行date[i] == date[j] 的上述 if 语句,但它不起作用,因为如果其他地方还有另一个 9oct 怎么办?
我的文本文件是:
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 <--
C++ 是否有数组对象,我可以这样做:
testArray['9oct16']
//EDIT// 尝试 Ayak973 的答案后,用 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;
【问题讨论】:
标签: c++ arrays for-loop g++ text-files