Taking all Integers from text file and add them

So, I've made a program where a user books a ticket. This ticket has a price/cost. Everytime a user creates a ticket, I add the cost of the price into a .txt file, which results in a .txt file with a lot of integers like so.
5
3
7
2
5
8
etc..

What I'm trying to do is, every time a user creates a ticket, it should add the cost together. Or make a way to get all the integers from the .txt file and add them up together to get a total sum.

The whole point of this is, so that I can know how much I've sold at the end of the day.

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
  void savingChild() {
	if (age <= 16 && levelInput == "beginner") {
		ofstream save("C:\\Customers\\Under-16 Beginner\\" + name + ".txt");
		save << "Name: ";
		save << name << endl; 
		save << "Age: ";
		save << age << endl; 
		save << "Class: ";
		save << classInput << endl;
		save << "level: ";
		save << levelInput << endl;
		save << "Total Cost: ";
		save << fixed << setprecision(2); save << "£" << totalCost << endl;
		save << "Time of Purchase: ";
		save << asctime(localtime(&ctt)) << endl;
		save.close();

		ofstream saveCost("C:\\Customers\\Overall Total.txt", ios_base::app | ios_base::out); // This is where the cost of this particular ticket goes.
		saveCost << totalCost << endl;

	}
	else if (age <= 16 && levelInput == "intermediate") {
		ofstream save("C:\\Customers\\Under-16 Intermediate\\" + name + ".txt"); 
		save << "Name: ";
		save << name << endl;
		save << "Age: ";
		save << age << endl;
		save << "Class: ";
		save << classInput << endl;
		save << "level: ";
		save << levelInput << endl;
		save << "Total Cost: ";
		save << fixed << setprecision(2) << (char)156; save << "£" << totalCost << endl;
		save << "Time of Purchase: ";
		save << asctime(localtime(&ctt)) << endl;
		save.close();

		ofstream saveCost("C:\\Customers\\Overall Total.txt", ios_base::app | ios_base::out); // This is where the cost of this particular ticket goes.
		saveCost << totalCost << endl;

	}
	else if (age <= 16 && levelInput == "advanced") {
		ofstream save("C:\\Customers\\Under-16 Advanced\\" + name + ".txt"); 
		save << "Name: ";
		save << name << endl;
		save << "Age: ";
		save << age << endl;
		save << "Class: ";
		save << classInput << endl;
		save << "level: ";
		save << levelInput << endl;
		save << "Total Cost: ";
		save << fixed << setprecision(2) << (char)156; save << "£" << totalCost << endl;
		save << "Time of Purchase: ";
		save << asctime(localtime(&ctt)) << endl;
		save.close();

		ofstream saveCost("C:\\Customers\\Overall Total.txt", ios_base::app | ios_base::out); // This is where the cost of this particular ticket goes.
		saveCost << totalCost << endl;
	}
}
Last edited on
some buyers may purchase more than 1 ticket and some buyers may purchase a combination of child and adult ticets. So, to retain greater flexibility in your program, a struct might be useful:
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";
}

search online if any of the methods is unfamiliar and then revert if any queries

Last edited on
Topic archived. No new replies allowed.