The code below takes the quantity of tickets from the 55 tickets that is available and it stores the value in a text file. What I want the program to do is to store the amount of tickets that is available and then each time the program executes it will take away from the remaining tickets that is available. Thanks

AmountOfTickets = 55; 

 NewAmountOfTickets = AmountOfTickets - AdditonalAdultQty - AdditonalChildQty - AdditonalSeniorQty - AdditonalFamilyQty - AdultQty - ChildQty - SeniorQty - FamilyQty;   
     
         ofstream myfile1;
 
         myfile1.open ("Tickets Left.txt");
 
         myfile1 << NewAmountOfTickets;
 
         myfile1.close();
        
         NewAmountOfTickets1 = NewAmountOfTickets - AmountOfTickets - AdditonalAdultQty - AdditonalChildQty - AdditonalSeniorQty - AdditonalFamilyQty - AdultQty - ChildQty - SeniorQty - FamilyQty;
        
         ifstream inFile("Tickets Left.txt");
 
         inFile>> NewAmountOfTickets;
 
         inFile.close();

Dani AI

Generated

The core problem here is that the program never reads the previously saved value and instead resets to the initial total each run. Follow idea (write a new file and replace the old one) but do the sequence in this order: 1) read the current remaining tickets from the file (if the file is missing or corrupted, fall back to the initial total), 2) subtract the new purchase total (validate and clamp to zero), 3) write the new remaining value to a temporary file and rename it over the original. This avoids the reset bug and makes the save operation safer.

Example flow (illustrative C++; change names/inputs to match your program):

```cpp
#include <fstream>
#include <string>
#include <cstdio>
#include <algorithm>

int readRemaining(const std::string &path, int initial) {
    std::ifstream in(path);
    int v = initial;
    if (in >> v) return v;
    return initial;
}

bool writeAtomic(const std::string &path, int value) {
    std::string tmp = path + ".tmp";
    std::ofstream out(tmp);
    if (!out) return false;
    out << value << '\n';
    out.close();
    if (std::rename(tmp.c_str(), path.c_str()) != 0) {
        std::remove(tmp.c_str());
        return false;
    }
    return true;
}


Notes and troubleshooting:
- Do the read before any arithmetic — do not reassign the initial total every run.
- Validate inputs (no negative ticket requests) and clamp the remaining tickets with `std::max(0, remaining - purchased)`.
- The temporary-file + rename pattern implements @Moschops suggestion and reduces corruption if the program crashes while saving. On POSIX the rename is atomic; on Windows it replaces only if no process has the file open.
- If multiple instances may run concurrently, add file locking (platform-specific) or use a small embedded database (SQLite) to avoid race conditions.
- Always check I/O errors and test the case where the saved file is missing or contains garbage.

This approach will keep the remaining ticket count persistent and avoid the double-subtraction / reset issues seen in the original snippet by @KRUX17.

Recommended Answers

All 2 Replies

Sounds like you want to delete a value from the file "Tickets Left.txt". To do this, you'll have to make a new file, copy everything across that you want copied across, delete the old one and then name the new one with the same name.

Could you show me an example of this? Thanks

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.