Why are you concatenating all of the inputs into a single string
? And why are you using a Map
for holding single items? I would suggest defining a struct
to hold the different fields you need, and then have your reading loop parse the individual lines into a vector
holding that struct
type, eg:
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
struct itemInfo
{
int quantity;
std::string name;
double price;
};
int main()
{
std::string line;
std::vector<itemInfo> items;
while (std::getline(std::cin, line))
{
try
{
itemInfo item;
auto start = line.find_first_not_of(" \t");
auto stop = line.find_first_of(" \t", start + 1);
item.quantity = std::stoi(line.substr(start, stop - start));
start = line.find_first_not_of(" \t", stop + 1);
stop = line.find(" at ", start);
item.name = line.substr(start, stop - start);
start = line.find_first_not_of(" \t", stop + 4);
item.price = std::stod(line.substr(start));
items.push_back(item);
}
catch (const std::logic_error &) { }
}
// use items as needed...
return 0;
}
Online Demo
You could then take this a step further by moving the parsing logic into an overloaded operator>>
for the struct
, eg:
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
struct itemInfo
{
int quantity;
std::string name;
double price;
};
std::istream& operator>>(std::istream &in, itemInfo &item)
{
try
{
std::string line;
if (std::getline(in, line))
{
auto start = line.find_first_not_of(" \t");
auto stop = line.find_first_of(" \t", start + 1);
item.quantity = std::stoi(line.substr(start, stop - start));
start = line.find_first_not_of(" \t", stop + 1);
stop = line.find(" at ", start);
item.name = line.substr(start, stop - start);
start = line.find_first_not_of(" \t", stop + 4);
item.price = std::stod(line.substr(start));
}
}
catch (const std::logic_error &)
{
in.setstate(std::ios_base::failbit);
}
return in;
}
int main()
{
itemInfo item;
std::vector<itemInfo> items;
while (std::cin >> item)
{
items.push_back(item);
}
// use items as needed...
return 0;
}
Online Demo