Hello,

I would like to know how you can keep running a function over and over again, so that it can keep adding the stored value. For example If Bob uses a program to purchase a ticket, lets say that Bob purchases 5 tickets out of 55 tickets (There's only 55 seats).So, now there is only 50 tickets left. Alice, comes and uses the program to purchase another 5 tickets. This will minus from the current value which is 50 and this will continue to happen until there's no tickets left and the program gives out an error message. How can I keep looping the function over and over again, so that different people can come and purchase any amount of tickets and then the program will take the quantity of the tickets and minus it from how many tickets there are left.

Thanks.

Dani AI

Generated

A practical pattern is to separate the purchase logic from the I/O loop: a small function should validate a requested quantity and only deduct from ticketsLeft when the request is valid. The loop that calls that function can keep running until tickets are gone or an operator quits. That implements the idea mentioned by and avoids the unsafe “always decrement” mistakes shown earlier by .

#include <iostream>
#include <limits>

bool tryPurchase(int &available, int qty) {
    if (qty <= 0 || qty > available) return false;
    available -= qty;
    return true;
}

int main() {
    int available = 55;
    while (available > 0) {
        std::cout << "Tickets left: " << available << "\nEnter quantity (0 to exit): ";
        int q;
        if (!(std::cin >> q)) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid input\n";
            continue;
        }
        if (q == 0) break;
        if (tryPurchase(available, q))
            std::cout << "Sold " << q << "; " << available << " left\n";
        else
            std::cout << "Cannot sell " << q << " (only " << available << " left)\n";
    }
    if (available == 0) std::cout << "Sold out\n";
    return 0;
}

Notes and common next steps:

  • Validate input (non-numeric, negative, zero) as shown to avoid corrupt state.
  • For multiple concurrent users (web or threaded app) don’t rely on an in-memory integer: use a database update like
    UPDATE shows SET available = available - ? WHERE id = ? AND available >= ?;
    then check affected rows = 1 to detect success and avoid oversell, or use a mutex/atomic in a single-process server.
  • Persist the remaining count (file or DB) so restarts don’t reset availability.

This approach keeps logic clear, prevents negatives, and handles the boundary cases that caused the original question from .

Recommended Answers

All 4 Replies

Use a loop. There are three kinds of loops: for loop, while loop, and do loop. Use the one that best fits the situation. Read this short tutorial

i usually use do{...}while(1);
1 is always true, therefore it will stay in the loop. if you want to get out of it, the command "break;" should do it.

Thanks for the replies, but could you show me what you mean?

Thanks again.

Thanks for the replies, but could you show me what you mean?

Thanks again.

if you code something like

int i;
do
{
    i++;
}
while(1);

it will jump into the do/while loop and start incrementing i (i++; -> i=i+1; ). after that it will look if the statment given in brackets is true or false. in c a true statement is 1 and a false one is 0. if the statement is true the loop will be looped once again, after that the statement is checked again.
you also could code while(i>0)... this also would be true for a start i=0;

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.