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

Dani AI

Generated

A few clarifications and practical patterns that fix the behaviour seen in the thread. was right that ios::app appends; was right that mixing a label and a number makes operator>> unreliable. The simplest, most robust choice is to pick one of two designs and stick to it: either maintain a single-file numeric total (overwrite it on every update), or keep an append-only log of individual sales and compute the total by summing the file. Merge the read/add/write steps into one function and avoid globals for the values being passed around.

Single-number (recommended for small apps): open the file for read, treat failure as zero, add the new sale, then open the same file for write (default mode truncates) and write only the integer followed by a newline. Do not use ios::app for the total file — that creates multiple numbers/labels and makes a single total impossible to read back.

Append-log (if an audit trail is required): append one numeric value per line and compute totals by reading the whole file and summing each line. Example helpers (robust against blank or malformed lines):

int sum_log(const std::string& path) {
    std::ifstream in(path);
    int total = 0;
    std::string line;
    while (std::getline(in, line)) {
        if (line.empty()) continue;
        std::istringstream ss(line);
        int v;
        if (ss >> v) total += v;
    }
    return total;
}

void append_sale(const std::string& path, int sold) {
    std::ofstream out(path, std::ios::app);
    out << sold << '\n';
}

Extra tips: always write a newline after numbers so appended lines don’t run together; default to 0 when the file is missing or unreadable; use a temp file + std::rename for atomic replacements if corruption is a concern; consider file locking or a small database (SQLite) if multiple processes/users can update simultaneously. Following ’s suggestion to pass the new-sale amount into a single update function will make the logic clear and avoid bugs caused by globals.

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

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.