jeudi 23 avril 2015

C++ Display unique date and its related price found in txt file

Currently i have a system that will store user input into a txt file, and have a function that will compute the daily sales of the month(Check by system date month) and display the sales of the day and the grand total of that month.

Take for example, the system date now check that the current month is Apr

Below are my current output:

20Apr2015 $11.5
20Apr2015 $15.5
22Apr2015 $4.5
25Apr2015 $4.5
28Apr2015 $4.5
20Apr2015 $4.5
     Grand Total $45

But my desired result should be as below.

20Apr2015 $31.5
22Apr2015 $4.5
25Apr2015 $4.5
28Apr2015 $4.5
     Grand Total $45

It should check that if there's an existing date, the value should add up.

Text File data:
1|Bag|2|3|6|20Apr2015 2|File|2.3|5|11.5|20Apr2015
3|File|2.3|5|15.5|20Apr2015
4|Book|0.9|5|4.5|22Apr2015
5|Pencil|0.9|5|4.5|25Apr2015
6|Rulur|0.9|5|4.5|20Jul2015
7|Eraser|0.9|5|4.5|20Jul2015
8|Bag|0.9|5|4.5|28Apr2015
9|File|0.9|5|4.5|20Apr2015

Below are my code for the section where i compute the Daily Sales. Can anyone guide me on how to get my desired output? Will appreciate alot.

            struct TransactionPile
            {
                // to record item information.
                string itemid;
                string itemdesc;
                float unitprice;
                int quantity;
                float totalprice;
                string date;
            };


            //Function to calculate daily sales
            void computeDailySales()
            {
                // current date/time based on current system
                time_t now = time(0);
                // convert now to string form
                char* ct = ctime(&now);
                // convert now to tm struct for UTC
                tm *ltm = localtime(&now);
                char* dt = asctime(ltm);

                string itemid;
                float totalprice;
                float totalsales = 0;
                string date;
                int year;
                int month;
                int day;
                int found;
                string syear;
                string smonth;
                string sday;
                string sdate;

                //Get the value of year
                year = (1900 + ltm->tm_year);   
                //Convert the year to string and store into syear
                syear = static_cast<ostringstream*>( &(ostringstream() << year) )->str();

                //Get the value of month
                month = (1 + ltm->tm_mon);
                //Convert the month to string and store into smonth
                smonth =static_cast<ostringstream*>( &(ostringstream() << month) )->str();

                //Get the value of day
                day = ltm->tm_mday;
                //Convert the day to string and store into sday
                sday = static_cast<ostringstream*>( &(ostringstream() << day) )->str();

                    //Store the different month name in smonth if condition met
                    if (smonth =="1")
                    {
                      smonth ="Jan";
                    }
                    if (smonth =="2")
                    {
                      smonth ="Feb";
                    }
                    if (smonth =="3")
                    {
                      smonth ="Mar";
                    }
                    if (smonth =="4")
                    {
                      smonth ="Apr";
                    }
                    if (smonth =="5")
                    {
                      smonth ="May";
                    }
                    if (smonth =="6")
                    {
                      smonth ="Jun";
                    }
                    if (smonth =="7")
                    {
                      smonth ="Jul";
                    }
                    if (smonth =="8")
                    {
                      smonth ="Aug";
                    }
                    if (smonth =="9")
                    {
                      smonth ="Sep";
                    }
                    if (smonth =="10")
                    {
                      smonth ="Oct";
                    }
                    if (smonth =="11")
                    {
                      smonth ="Nov";
                    }
                    if (smonth =="12")
                    {
                      smonth ="Dec";
                    }
                //Concatenate the sday, smonth, syear and store inside sdate
                sdate = sday + smonth + syear;



                cout<<endl;
                for(int i = 0; i < MAX- 1; i++)
                    {
                    if(transactionpile[i].itemid !="")
                    {
                        //Find all related record according to the value in smonth
                        found = transactionpile[i].date.find(smonth);
                        if(found<transactionpile[i].date.length())
                        {
                            cout << transactionpile[i].date << "             " << transactionpile[i].totalprice <<  endl;
                            //Add the totalprice of each found records to totalsalesmonth[k]
                            totalsales += transactionpile[i].totalprice;

                        }


                    }

                }
                //Print out the totalsales made on the day itself by system date
                cout << "            " << "Total Sale" << endl;
                cout << "            " << totalsales <<endl;
            }

Aucun commentaire:

Enregistrer un commentaire