Hello,

I'm working on a project and the value of num is AdditonalAdultQty + AdultQty (AdditonalAdultQty and AdultQty will be inputted from the user) Then those two values will be stored in the text file. When the program runs again and a different user will input how many tickets he would like then AdditonalAdultQty + AdultQty will be added together and that value will be added to the stored data in the text file. The purpose of this is to find out how many tickets are sold for the adult category for the day. I don't know what I'm doing wrong with the code. How can I do this?

void SoldTickets ()

{

            TotalAdultTicketsSold = AdditonalAdultQty + AdultQty;

            ofstream myfile;

            myfile.open ("Sold Tickets.txt");

            myfile << "Sold tickets for adult:"<< TotalAdultTicketsSold;

            myfile.close();

          

             

                   

            fstream indata("Sold Tickets.txt",ios::in);

             

            float num;

             

            indata >> num;

            TotalNo = num + TotalAdultTicketsSold;

            indata << TotalNo;
             

            indata.close();

Thanks

Recommended Answers

All 8 Replies

To append data to the existing file you have to add another parameter to the open() statement myfile.open ("Sold Tickets.txt",ios::app); See the options in this link

Thanks for the reply. I've managed to add the ios::app but when I look at my text file it shows: Sold tickets for adult:6Sold tickets for adult:5. What did I do wrong? Thanks

void SoldTickets ()
{
            
     
     
            TotalAdultTicketsSold = AdditonalAdultQty + AdultQty;
 
            ofstream myfile;
 
            myfile.open ("Sold Tickets.txt",ios::app);
 
            myfile << "Sold tickets for adult:"<< TotalAdultTicketsSold;
 
            myfile.close();
 
         
}




void SoldTickets1 ()
{    
     
    int TotalAdultTicketsSold;
    ifstream openfile ("Sold Tickets.txt");
    openfile >> TotalAdultTicketsSold;
    NewTotal = TotalAdultTicketsSold + AdultQty + AdditonalAdultQty; 
    cout <<"Hello" <<  NewTotal << endl;
        
     
     
     
    
    
}

If im reading your code correctly, you are adding to the file first a string then an int.

So when you read back from the file you are writing a string into an int and im not 100% sure how iostreams handle that.

Try just outputting the int to the file on its own without the string.

If you need the string to be there then you will have to decide how you are going to parse the line to get the pieces of information individually.

Thanks for the reply. I did what you told me to do, but it's not what I wanted. I wanted to add the old value which is written to the text file. So, when another user uses the program it will add old value with the new value. I'm trying to find out the total tickets that are sold for the day.

void SoldTickets ()
{
            
     
     
            TotalAdultTicketsSold = AdditonalAdultQty + AdultQty;
 
            ofstream myfile;
 
            myfile.open ("Sold Tickets.txt",ios::app);
 
            myfile << TotalAdultTicketsSold;
 
            myfile.close();
 
         
}




void SoldTickets1 ()
{    
     
    int TotalAdultTicketsSold;
    ifstream openfile ("Sold Tickets.txt");
    openfile >> TotalAdultTicketsSold;
    NewTotal = TotalAdultTicketsSold + AdultQty + AdditonalAdultQty; 
    cout <<"Hello" <<  NewTotal << endl;
        
     
     
     
    
    
}

In what order are the functions called? are the variables global or arguments to the functions?

Also one function blindly over writes the value in the file the other gets from the file but does not update the file contents.

The variables are global to the functions. I'm really lost with this sorry.

Well really you have all the logic you need, but its split across two functions, merge the functions together and you will have what you need in one function call.

Also for this kind of problem there's no need for globals so try get in the habbit of passing the amount of extra tickets sold as an argument so something like this

void UpdateSavedTicketCountFile( int nNumTicketsJustSold )
{
   int nNumberOfTicketsSoldPrev;
   int nNumberOfTicketsSoldNew;

   /* get current value from file */
   ifstream inFile("Sold Tickets.txt");

   inFile>> nNumberOfTicketsSoldPrev;

   inFile.close();

   /* calculate new total */
   nNumberOfTicketsSoldNew = nNumberOfTicketsSoldPrev + nNumTicketsJustSold;
   
   /* save new total to file */
   ofstream outFile("Sold Tickets.txt");

   outFile <<  nNumberOfTicketsSoldNew << endl;

   outFile.close();
}

Thanks it works, but how do I keep on adding the value, so that when someone else uses the program the tickets that are sold will be added to the old value. I'm not sure, but a while loop might do the trick for this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.