for循环中来自textfile的C ++和值

时间:2016-10-23 06:39:50

标签: c++ for-loop text-files

cout<<"Enter the date(Format eg. 8oct16   , enter 1 to Main Menu):";
cin>>dateChoice;
if(dateChoice == "1")
{ displaymainmenu(); return;} 

string samequantityitemName;
int samequantity =0;

for(int i=0;std::getline(myfile,myline);i++) 
{
    if(sp[i].datepurchased == dateChoice) // let say i input 9oct16 , retrieve all info that date = 9oct16
    {   

        cout<<sp[i].itemdesc<<sp[i].quantitypurchased<<endl; // this should print all values that date = 9oct16

             for(int j=0;std::getline(myfile,myline);j++)
            {
                if(sp[i].itemdesc == sp[j+1].itemdesc) // check every line if item desc is the same
                    {
                        samequantity = sp[i].quantitypurchased; 
                        samequantity +=sp[j+1].quantitypurchased; //adding all quantitypurchased;
                        samequantityitemName = sp[i].itemdesc;
                    } // end of if

            } // end of the 2nd for loop

            cout<<samequantity<<"    "<<samequantityitemName<<endl; // **DOESN"T WORK** //


    } // end of if statement for user input
} // end of main for loop

基本上我有一个如下所示的文本文件:

itemid:itemdesc:unitprice:quantitypurchased:datepurchased

22:blueberries:3:9:9oct16
11:apples:8:7:9oct16
16:melon:9:10:9oct16
17:melon:9:11:9oct16
18:melon:9:12:9oct16
19:melon:9:13:9oct16
67:pineapple:123:12:10oct16
44:po:9:9:10oct16
63:juicy:11:11:10oct16
5:starfruit:6:2:10oct16
5:kiwi:6:2:16oct16

我正在尝试获取此输出(当datepurchased为9oct16时):

blueberries 9
appples 7
melon 46

但我得到的输出是:

blueberries 9

我正在尝试显示用户输入的特定日期的所有商品描述和购买数量。对于重复项目,我将累计数量,并且只打印重复项目的单行,并购买总计数量。我在这里出了什么问题?是我的登录流向错误的方向?

代码太长,无法发布sp但例如sp[1].itemdesc从文本文件的第一行给出了itemdesc( blueberries )。

0 个答案:

没有答案