1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
struct TicketSale
{
size_t m_adultTix = 0;
double m_adultPrix = 0.0;
size_t m_childTix = 0;
double m_childPrix = 0.0;
TicketSale(const size_t adultTix, const double adultPrix, const size_t childTix = 0, const double childPrix = 0)
: m_adultTix(adultTix), m_adultPrix(adultPrix), m_childTix(childTix), m_childPrix(childPrix){}
double totalCost()const
{
return m_adultTix * m_adultPrix + m_childTix * m_childPrix;
}
};
std::ostream& operator << (std::ostream& os, const TicketSale& t)
{
os << t.m_adultTix << " " << t.m_adultPrix << " : " << t.m_childTix << " " << t.m_childPrix << "\n";
return os;
}
int main()
{
std::ofstream outFile{"F:\\test1.txt"};
std::vector<TicketSale> allTickets{};
allTickets.emplace_back(TicketSale{2, 18, 3, 12}); //2 adults (@ 18) + 3 children (@ 12)
allTickets.emplace_back(TicketSale{3, 22}); // 3 adults (@ 22) + 0 children (@ 16)
double sum{};
for (const auto& elem : allTickets)
{
std::cout << elem; //prints to screen
outFile << elem; //prints to file
sum += elem.totalCost(); // running total
}
std::cout << sum << "\n";
sum = 0;
outFile.close();
std::ifstream inFile{"F:\\test1.txt"};
std::vector<TicketSale> inputTickets{};
std::cout << "\nFrom input file: \n";
if(inFile)
{
std::string line{};
while(getline(inFile, line))
{
std::istringstream stream{line};
size_t adultTix{};
double adultPrix{};
char dummy{};
size_t childTix{};
double childPrix {};
while (stream)
{
stream >> adultTix >> adultPrix >> dummy >> childTix >> childPrix;
}
if(inFile)
{
inputTickets.emplace_back(TicketSale(adultTix, adultPrix, childTix, childPrix));
}
}
}
for (const auto& elem: inputTickets)
{
std::cout << elem ;
sum += elem.totalCost(); // running total
}
std::cout << sum << "\n";
}
|