Search code examples
c++stringvectorintegerdouble

C++ read data types string int and double using Vector Pair


The problem is that i have to read a file that includes:

 type     count    price

bread      10       1.2 
butter      6       3.5
bread       5       1.3
oil        20       3.3
butter      2       3.1
bread       3       1.1

I have to use Vector Pair to read the file and to multiply the count and price and the output should be :

oil     
66
butter 
27.2
bread   
21.8

Any idea would be highly appreciated!


Solution

  • If you only want to use std::pair and std::vector then you could use the following program as a starting point(reference):

    Version 1: Product names will be repeated

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <vector>
    int main()
    {
        std::ifstream inputFile("input.txt"); //open the file
        std::string line; 
        
        std::vector<std::pair<std::string, double>> vec;
        
        std::string name;
        double price, count;
        
        if(inputFile)
        {   std::getline(inputFile, line, '\n');//read the first line and discard it
            while(std::getline(inputFile, line, '\n'))//read the remaining lines
            {
                std::istringstream ss(line);
                ss >> name; //read the name of the product into variable name
                ss >> count;//read the count of the product into variable count
                ss >> price;//read the price of the product into variable price
                
                vec.push_back(std::make_pair(name, count * price));
            }
        }
        else 
        {
            std::cout<<"File cannot be opened"<<std::endl;
        }
        inputFile.close();
        
        //lets print out the details 
        for(const std::pair<std::string, double> &elem: vec)
        {
            std::cout<< elem.first<< ": "<< elem.second<<std::endl;
        }
        return 0;
    }
    
    

    You can/should instead use a class or struct instead of using a std::pair.

    The output of the above program can be seen here. The input file is also attached in the above link. The output of the above version 1 is:

    bread: 12
    butter: 21
    bread: 6.5
    oil: 66
    butter: 6.2
    bread: 3.3
    

    As you can see in the output of version 1 the names of the product are repeated. If you don't want the repeated names and want the values correpsonding to the repeated keys summed up, check out the below given version 2:

    Version 2: Product names are not repeated

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <vector>
    int findKey(const std::vector<std::pair<std::string, double>> &vec, const std::string &key)
    {   int index = 0;
        for(const std::pair<std::string, double> &myPair: vec)
        {
            if(myPair.first == key)
            {
                return index;
            }
            ++index;
        }
        return -1;//this return value means the key was not already in the vector 
    }
    int main()
    {
        std::ifstream inputFile("input.txt");
        std::string line; 
        
        std::vector<std::pair<std::string, double>> vec;
        
        std::string name;
        double price, count;
        
        if(inputFile)
        {   std::getline(inputFile, line, '\n');
            while(std::getline(inputFile, line, '\n'))
            {
                std::istringstream ss(line);
                ss >> name; 
                ss >> count;
                ss >> price;
                int index = findKey(vec, name);
                if(index == -1)
                {
                    vec.push_back(std::make_pair(name, count * price));    
                }
                else 
                {
                    vec.at(index).second += (count *price);
                }
                
            }
        }
        else 
        {
            std::cout<<"File cannot be opened"<<std::endl;
        }
        inputFile.close();
        
        //lets print out the details 
        for(const std::pair<std::string, double> &elem: vec)
        {
            std::cout<< elem.first<< ": "<< elem.second<<std::endl;
        }
        return 0;
    }
    
    

    The output of version 2 is

    bread: 21.8
    butter: 27.2
    oil: 66
    

    which can be seen here.