Skip to content

Instantly share code, notes, and snippets.

@MORTAL2000
Last active May 11, 2018 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MORTAL2000/b95707890c90e1463731b63f386e43c4 to your computer and use it in GitHub Desktop.
Save MORTAL2000/b95707890c90e1463731b63f386e43c4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
/* test.txt
Plain Egg
1.45
Bacon and Egg
2.45
Muffin
0.99
French Toast
1.99
Fruit Basket
2.49
Cereal
0.69
Coffee
0.50
Tea
0.75
*/
struct menuItems
{
std::string item;
double price;
};
int main()
{
std::vector<menuItems> menuOrders;
std::ifstream fin("test.txt");
std::string temp;
double price;
int i = 0;
while (std::getline(fin, temp))
{
if (i++ % 2 == 0)
menuOrders.emplace_back(menuItems{});
if (std::stringstream(temp) >> price)
menuOrders.back().price = price;
else
menuOrders.back().item = temp;
}
fin.close();
for (const auto& order : menuOrders)
{
std::cout << std::setw(20) << std::left << order.item;
std::cout << std::setw(10) << std::right << order.price << '\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment