Hi,

I was wondering is there a way where you can sell limited tickets. For example there are 55 seats in a bus and 55 people bought the tickets for the bus,the people who want to buy those tickets for the bus must wait for an hour. How would I implement this code in Dev c++ . Thank you.

Dani AI

Generated

Short version for (and following up on ): stop treating the whole day as a single counter. Model each scheduled departure (9:00, 10:00, ... up to 17:00) with its own remaining-seat count in persistent storage. On each program run read that per-departure state, pick the next departure at or after the current time, and decrement that departure’s seats only if enough remain. Do not block the program with a one‑hour sleep — compute the next available departure time and return that to the user.

A simple workable flow you can implement in Dev-C++:

  1. Store a small file like schedule.csv where each line is "minutes_since_midnight,seats" (09:00 → 540).
  2. On start read the file into a map<int,int>.
  3. Find the earliest key >= now that has seats > 0. If none, tell the user no seats today. If found and seats >= request, subtract and save the map back to disk. Otherwise offer the next departure with enough seats.

Example outline (short, original) — tweak for your variable names and input validation:

/* load schedule.csv into std::map<int,int> schedule */
int now = now_minutes(); // compute hour*60+min
auto it = std::find_if(schedule.begin(), schedule.end(),
                       [&](const auto& p){ return p.first >= now && p.second > 0; });
if (it == schedule.end()) {
  // no seats today
} else if (it->second >= requestedSeats) {
  it->second -= requestedSeats;
  saveSchedule("schedule.csv", schedule);
  // confirm sale for time it->first
} else {
  // not enough on this bus — find next with enough seats or offer fallback
}

Troubleshooting and hardening: if multiple users/processes may run concurrently, switch to SQLite (atomic transactions) or implement an OS-level lock. Use atomic file replace (write temp then rename) to avoid partial writes. Test boundary cases (exact departure time, midnight reset, daylight saving) and always validate requested quantity before committing the write.

Recommended Answers

All 5 Replies

Why would they need to wait for an hour?
I must be missing something in the explanation.

Are you having problems with timing in the code or ticket sales or not showing the menu for an hour or ???

Thanks for the reply.The bus starts at 9am and every next bus comes in an hour and at 5pm the bus stop closes. So, I would just like to know how I can start it off.

I figured out how I can sell limited tickets, but if the tickets are sold out then they must wait for an hour, how would I do this? Thanks

That depends on how it will recognize the no-longer-sold-out condition.
If you really mean it should wait for an hour, you will need to:
0) Detect the sold-out condition
0a) Tell the user the tickets are sold out.
1) get the system time
2) if not previously in a sold-out condition: add an hour to that in some "other" variable
3) wait for the next request
3a) see if the system time is greater than or equal to the "other" variable
3b) if so, check to see if tickets are available
3c) if not, jump to step 1 (in this sequence)
4) Sell the user a ticket.

Now, the problem comes in when you detect the sold-out condition.
What are you checking to determine tickets are currently available?
If there is no proper check, you wil be stuck in a time loop of 1 hour every time a request comes in.

Is that what you mean?

Yes, that's what I meant thank you. 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

int 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();
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.