Ordering numbers

So I got this little function. This function is used to save a ticket into a .txt file. What I'm trying to achieve is, for every time I finish making a ticket, I want to add a number next to the name. The function is inside a main() for(;;) loop.
This is what I'm trying to make my text file look like:

1. Name: Michael
Age: 13
Class: salsa
level: beginner
Total Cost: £2.50
Time of Purchase: Tue May 16 17:10:18 2017

2. Name: Mike
Age: 13
Class: salsa
level: beginner
Total Cost: £2.50
Time of Purchase: Tue May 16 17:10:18 2017

This is how my text file currently looks like, I want it to be 2 instead of 8 etc:

1. Name: Michael
Age: 13
Class: salsa
level: beginner
Total Cost: £2.50
Time of Purchase: Tue May 16 17:10:18 2017

8. Name: Mike
Age: 13
Class: salsa
level: beginner
Total Cost: £2.50
Time of Purchase: Tue May 16 17:10:18 2017

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
  void savingChild() {
	if (age <= 16 && levelInput == "beginner") {
		int uniqueID = 1;
		string readFile;
		string prepareText;

		ifstream ticketOrders("C:\\Customers\\Child Beginner\\Orders.txt");

		if (ticketOrders.is_open())
		{
			while (getline(ticketOrders, readFile))
			{
				++uniqueID;
				prepareText = prepareText + readFile + "\n";
			}
			ticketOrders.close();
		}


		ofstream save("C:\\Customers\\Child Beginner\\Orders.txt");

		save << prepareText << to_string(uniqueID) << ". ";
		save << "Name: ";
		save << name << endl; //Takes the name string
		save << "Age: ";
		save << age << endl; //Takes the age integer
		save << "Class: ";
		save << classInput << endl;
		save << "level: ";
		save << levelInput << endl; //Takes the levelInput in string
		save << "Total Cost: ";
		save << fixed << setprecision(2); save << "£" << totalCost << endl; //Saves the total cost
		save << "Time of Purchase: ";
		save << asctime(localtime(&ctt)) << endl; //Saves date and time
		save.close();
	}
Last edited on
Bump
Lines 12-14: uniqueID is going to get incremented for every LINE you read from the input file.
You want to increment uniqueID only when you find a line that contains "Name:".
How should I approach making it only increment uniqueID only when I find a line that contains "Name:"
Thank you for clarifying that, I'm new to c++ and that is a college project.
Last edited on
Hello Viston,

It is hard to suggest what you can do to fix your function with knowing what the "Orders.txt" fie looks like. The code that is missing would be helpful to.

At first look what I can say is that the while loop to read the file is all wrong. The "prepareText" variable will end up holding the entire file that is read by the while loop. And the "uniqueID" will hold the total lines read by the while loop, so when you print "uniqueID" it will be total lines read not the 2 that you want.

As your code is now it only extracts a single line from the file and does nothing to give variables like "name", "age" etc. a value.

The lines 22 - 34 only print to the file once after the while loop is finished. And I do not see where any of those variables, except "uniqueID", get any new values.

When the time comes I would move line 20 to line 8. Move lines 22 - 34 inside the while loop, so that after you break up the line read into the different variables you can print them to the output file before reading the next line. The use of a string stream inside the while loop would prove useful. Not knowing the rest of the program I do not know where or how the variables "name", "age", etc. are defined or how this function knows about them.

Hope that helps,

Andy
Topic archived. No new replies allowed.