mercredi 29 avril 2020

How to read from a text file and put it into a map? c++

So I have a text file that I want to read and then put it into a map. Right now, I have written this, but I know this isn't correct.

   #include "headers.h"

   void SearchItem(string itemName);
   void SearchItem(double price);
   void SearchItem(int amount);
   void sort();

   struct ItemInfo{
      double Itemprice = 0;
      int Itemamount = 0;
   };

   struct storeName{
      string StoreName;
      map<string, ItemInfo> storeItem;
   };

   class inventory
   {

   public:
   //constructor to read frmo file
     inventory();
   //function to add items in map
     void updateInventory( int items);
  //function to iterate throught the list
     void iter();
  //last store item entered
     void viewInventory(string SName);
  //get list
     list<storeName> get_list();
  private:
     list<storeName> stores;
  };

I have written this class which has a list of type storeName which is a struct. I used a struct because I am trying to make an inventory list of all local grocery stores in my area and then have a map of their items like fruits and vegetables along with their prices and amount available in the store. The list is going to store the store name and the map, the map is going to store the item name, which will be the key and then two values- price and amount. I used a struct for map too so it can hold two values and then in the future, I could search by storename or throught the key. In the class, I am using the constructor as my read from file function which is this

  inventory::inventory(){
//read from file function
     ifstream read;
     vector<string> name;
     storeName input;
     ItemInfo enter;
     auto iter = stores.begin();
     auto mapIter = iter->storeItem.begin();
     iter->StoreName = "Walmart";
     read.open("/Users/vibhorsagar/Desktop/CovidProject/CovidProject/walmartList.txt");
     if(!read.is_open()){
        cout<<"Error"<<endl;
        return;
     }else {
         while(!read.eof()){
             string line;
             double price;
             int amount;
             getline(read, line, ' ');
             stringstream in(line);
             in>>line;
             name.push_back(line);
               while(in>> price >> amount){
                     //stuck here    
              }   
         }    
     }
 }

Now the in the text file I have this

Marketside Butter Lettuce Salad 2.98 200
Marketside Classic Iceberg Salad 2.64 400

so the name of the item is long and I want to store it as the key when I read it from the file. If it was just one name like "potato" or "tomato" then It would be kinda easier, but since the name is long and the price is double and the amount is int, I don't know how to store it in a map. Any suggestions would be helpful and if you have any suggestions on how I could implement it better in my code, or make my code better in general, it would be really helpful. Thank you

Aucun commentaire:

Enregistrer un commentaire