Skip to content

Instantly share code, notes, and snippets.

@MORTAL2000
Last active May 11, 2018 16:40
Show Gist options
  • Save MORTAL2000/b95707890c90e1463731b63f386e43c4 to your computer and use it in GitHub Desktop.
Save MORTAL2000/b95707890c90e1463731b63f386e43c4 to your computer and use it in GitHub Desktop.

Revisions

  1. MORTAL2000 revised this gist May 11, 2018. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions reading_file-line_by_line.cpp
    Original file line number Diff line number Diff line change
    @@ -42,9 +42,7 @@ int main()
    while (std::getline(fin, temp))
    {
    if (i++ % 2 == 0)
    {
    menuOrders.emplace_back(menuItems{});
    }

    if (std::stringstream(temp) >> price)
    menuOrders.back().price = price;
  2. MORTAL2000 renamed this gist May 11, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. MORTAL2000 created this gist May 11, 2018.
    62 changes: 62 additions & 0 deletions readig_file.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    #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';
    }
    }