0

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;
what
  • 373
  • 2
  • 10
  • 20

2 Answers2

2

With c++11 support, you can use std::unordered_map to store key/values pair:

#include <string>
#include <unordered_map>

std::unordered_map<std::string, int> totalMap;

//...

for(i=0;std::getline(myfile,myline);i++) { 
    auto mapIterator = totalMap.find(stockpile[i].datepurchased); //find if we have a key
    if (mapIterator == totalMap.end()) {  //element not found in map, add it with date as key, unitPrice as value
        totalMap.insert(std::make_pair(stockpile[i].datepurchased, stockpile[i].unitprice));
    }
    else { //element found in map, just sum up values
         mapIterator->second += stockpile[i].unitprice;
    }
}

After that, you got one map with date as keys, and sum of unit price as values. To get the values, you can use range based for loop :

for (auto& iterator : totalMap) {
    std::cout << "Key: " << iterator.first << " Value: " << iterator.second;
}
Ayak973
  • 468
  • 1
  • 6
  • 23
  • i have to do g++ -std=c++11 Main.cpp to compile and it gives me compilation error see my edit @Ayak973 – what Oct 19 '16 at 01:44
  • @user2601570 : My bad, i didn't test the code, I've edited my post, just use `mapIterator->second` – Ayak973 Oct 19 '16 at 06:35
  • Thanks but it prints out from the last line of the textfile .it suppose to work this way ? – what Oct 20 '16 at 02:12
  • Is it possible to have 2 keys? for example itemdesc and datepurchased @Ayak973 i tried makepair((itemdesc,datepurchased),unitprice) , don't seems to work. – what Oct 20 '16 at 05:34
  • 1
    @user2601570 : if you want two keys, you can use a `std::pair` or a custom `struct`, but you need to supply a hash function object and a predicate for comparing equality of your keys. You can read [this](http://stackoverflow.com/a/17017281/5847906) to help you – Ayak973 Oct 20 '16 at 06:47
0

You can use hash tables implemented in following link https://gist.github.com/tonious/1377667 Or you can easily define class ,which have hash key and value, and define array of the class.

Ali sahin
  • 21
  • 3