A small problem with retrieving info from a txt file.

Hello everyone,
I have a program where i have to take inputs for tickets
each ticket has a unique number.
and the ticket number range goes from 1 to 51.
so i decided to number each new ticket+1 so 1,2,3,4,5,6..etc
then we have to save it in a file.
i got everything right the program does its job but the thing is when i exit the program and run it again, the count resets, i want the program to read the last ticket number i registered then increment it by 1 and complete the range which is x<51.
is that possible ?


Thanks in advance.
Where are you storing the tickets, once you've created them?
- in a single file?
- in (upto) 51 separate files named ticketNN.txt

You could
- examine the data already created to see what the last ticket ID was
- store the last ticket ID in a file.
nope,
in a single file.
that being said i have other types of tickets too with different ranges, 1 to 51 is gold 51 to 200 is platinium..etc
so how do i exactly examine the data already created to see what the last ticket ID was ?
Depends on the format of the data you wrote.

It's your file you're writing. Did you consider how hard it might be to read it back in?

File reading, file writing and file parsing are all pretty much learnt at the same time.
yeah the format of the data is
int string string string
int (is the number of the ticket which is i am trying to increment) the rest strings are the customer's info)
the thing here is i am not very good at files
man i give up
the whole program works perfectly but restarts the whole count upon restarting the program
i guess i can not solve this thing.
Post your code.

To get the new count, open the existing file and read the value of the last ticket and use that as a reference to start your new count.

Don't give up so easily. I'm pretty sure everybody here has been stuck on a problem for more than two hours before.
Last edited on
The code is pretty much a mess, a big mess.
but i will post it, it has tooo many loops
i hope you give few minutes to try to run it and inform me of where did it go wrong
i figured out how to increment but it really goes wrong, at some point it starts adding more than 1.
while i know that i have added too many things and too many loops but starting again is not an option.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream in;
ofstream out;
string info, name1;
int TC; //TC stands for "Ticket Category
in.open("tickets.txt", ios::app);
out.open("tickets.txt",ios::app);
char entry;
int countP=1, countG=51, countS=201, countB=401, reopen=1, TCreopen, countUn, phoneUn, idUn, PlatinumTno=0, GoldTno=0, SilverTno=0, BronzeTno=0;

while(in>>countUn>>idUn>>name1>>phoneUn){
if (countUn>=1 && countUn<=50){PlatinumTno++;}
else if (countUn>=50 && countUn<=200) {GoldTno++;}
else if (countUn>=200 && countUn<=400) {SilverTno++;}
else if (countUn>=400 && countUn<=1000) {BronzeTno++;}}
do{
do
{ cout<<"The main menu:\n a. Issue a new ticket\n b. Cancel ticket reservation\n c. Print out customer purchase\n d. Show income statistics\n e. Choose random winner\n f. Exit\n";
cin>>entry;}
while (entry != 'a' && entry != 'A');
do {
if (entry == 'a' || entry == 'A'){
do{ cout<<"The options for ticket reservation are: \n 1. Platinum\n 2. Gold\n 3. Silver\n 4. Bronze\n";
do{cin>>TC;
cin.ignore(); //This is the only way we found to make getline(cin, info) work proberly
if (TC==1 && countP <=50){
if(PlatinumTno!=0){countP= countP + PlatinumTno;}
cout<<"Enter the id, firstname and phone of the customer consecutively in one line.\nfor example: 123456789 Ahmed 12345678\n";
getline(cin, info);
cout<<"You have successfully booked a Platinum ticket.\n\n";
out<<countP<<" "<<info<<endl;countP++;}
else if(TC==1 && countP>50) {cout<<"There is no more bookable platinum tickets "<<endl;}
if (TC==2 && countG<=200)
{if(GoldTno!=0){countG= countG + GoldTno;}
cout<<"Enter the id, first name and phone of the customer consecutively in one line.\nfor example: 123456789 Ahmed 12345678\n";
getline(cin, info);
cout<<"You have successfully booked a Gold ticket.\n\n";
out<<countG<<" "<<info<<endl;countG++;}
else if(TC==2 && countG>200){ cout<<"There is no more bookable Gold tickets "<<endl;}
if (TC==3 && countS<=400){if(SilverTno!=0){countS= countS + SilverTno;}
cout<<"Enter the id, first name and phone of the customer consecutively in one line.\nfor example: 123456789 Ahmed 12345678\n";
getline(cin, info);
cout<<"You have successfully booked a Silver ticket.\n\n";
out<<countS<<" "<<info<<endl;countS++;}
else if(TC==3 && countS>400){ cout<<"There is no more bookable Silver tickets"<<endl;}
if (TC==4 && countB<=1000){if(BronzeTno!=0){countB= countB + BronzeTno;}
cout<<"Enter the id, first and phone of the customer consecutively in one line.\nfor example: 123456789 Ahmed Khaled 12345678\n";
getline(cin, info);
cout<<"You have successfully booked a Bronze ticket.\n\n";
out<<countB<<" "<<info<<endl;countB++;}
else if(TC==4 && countB>1000){ cout<<"There is no more bookable Br tickets"<<endl;}
if (TC!=1 && TC!=2 && TC!=3 && TC!=4) cout<<"Invalid choice, choose again.\n";}
while(TC != 1 && TC != 2 && TC != 3 && TC != 4);

}
while (TC != 1 && TC != 2 && TC != 3 && TC != 4);}
cout<<"Do you want to book a new or different ticket ?\n1.Yes \n2.No ";
cin>>TCreopen;} while(TCreopen==1);
}while (reopen==1);
out.close();
return 0;}






Last edited on
Topic archived. No new replies allowed.