Hello,

I'm quite stuck on how I can sell limited tickets I've put this question up before, but I haven't got the right solution.I know I should do my own project, but I really can't seem to find the solution to this.If someone can just help me with this problem that would be great and then I don't have to spent countless hours researching for nothing. The Problem: There are only 50 tickets available. Now, when a person purchases one ticket this there should be only 49 tickets left. Lets say another person purchases two tickets there should be only 47 tickets left. So, after all the tickets are gone it should give out an error message to the user saying that there is no more tickets left. If someone can just put me on the right track that would be really great.


Thanks

This seems like such a simple and basic code to write on the face of it, it's hard to understand what exactly you are having trouble with.

Please provide your attempt.

Thank you for your reponse. I thought maybe using a for loop would help.

void getQuantityTickets(int &AdultQty, int &ChildQty, int &SeniorQty,int &FamilyQty) 
{
    int NumberOfTickets,NewNumberOfTickets;
    int i = 55; 
     
	cout<<"Please enter the number of adult tickets you would like:";
	cin >> AdultQty;
	cout<<"Please enter the number of child tickets you would like:";
	cin >> ChildQty;
	cout <<"Please enter the number of senior tickets you would like:";
	cin >>SeniorQty;
	cout <<"Please enter the number of family tickets you would like:";
	cin >>FamilyQty;
	NumberOfTickets = AdultQty+ChildQty+SeniorQty+FamilyQty; 
	cout << "Total Qtys " << NumberOfTickets << endl;
	
  do {	
	for (i; i > NumberOfTickets;i--)
	{
        NewNumberOfTickets = i - NumberOfTickets;
        cout << NewNumberOfTickets << endl;
	
    }
   
   }
   while (NewNumberOfTickets == 0);
	           
	
}

Why can't you subtract the [number of tickets entered] from the [number of tickets left] (which starts at 50) and check if the new [number of tickets left] is < 0?

if its <0 you went over, better off just using a for loop to substract the value starting @ 50 then substracting -1 at a time (for int i=50; i > 0; i--)

i-- means subtract 1 from i once at the end of loop.

basic sytax;

for(initial values; while something is true; counter either ++ or --)

you can also do:

for(int i=0; i < 50; i ++){
//some code here.
}


Edit:

Change Following:

do {
for (i; i > NumberOfTickets;i--)
{
NewNumberOfTickets = i - NumberOfTickets;
cout << NewNumberOfTickets << endl;
 
}
 
}
while (NewNumberOfTickets == 0);

To:

int counter = 0;
do {
cout << NewNumberOfTickets - counter << endl; // Will Print Tickets
counter ++; // Increments Counter by 1 everytime the loop goes through.
}while (counter < 50);

That should be a fix for you, First Part of my post was half brain dead.

Edited 4 Years Ago by ilearnz001: Bad Attempt @ Reading

You need to rethink your program. If you don't understand what you are writing, you should not be writing code. You need to sit at a desk with pencil and paper and plan -- yes plan -- what you need to accomplish.

Write down what the task is.
Break that description into pieces -- like in English class, what are the nouns, verbs, and the implied parts (but don't really look for nouns and verbs -- that's English class)

Write down how you accomplish each task. Then break that down into smaller pieces until you get to the point each step is as small as it can get.

There's your design. Now run through it by hand to see if it does what you want. Write down variables as they change. YOU be the computer and that description is your code.

When that's done, works, etc., now you're ready to start coding. Take a section at a time. Do the input. Get it working completely. Add the output. Get it working. Then one more step. Get it working. Add another... ad nauseum.

That's programming! (sorry, but that's really how we do it.)

Credits to me, here you go:

int CountUnsoldTicket = 50,
	CountSoldAdultTicket = 0,
	CountSoldKidTicket = 0,
	CountSoldSeniorTicket = 0,
	CountSoldFamilyTicket = 0,
	CountSoldTicket = 0;

void BuyTicket(string TicketDealerMsg, int &CountPurchasingTicket, int &TicketType)
{
	if(CountUnsoldTicket > 0)
	{
		cout << endl << TicketDealerMsg << endl;
		cin >> CountPurchasingTicket;
		if(CountPurchasingTicket < 0 || CountPurchasingTicket > CountUnsoldTicket)
		{
			cout << "Error! Can not be lower than 0 or more than " << CountUnsoldTicket << endl;
			system("Pause");
			exit(2);
		}
		else if(CountPurchasingTicket > 0)
		{
			CountUnsoldTicket -= CountPurchasingTicket;
			TicketType += CountPurchasingTicket;
			CountSoldTicket += CountPurchasingTicket;
			CountPurchasingTicket = 0;
		}
		cout << endl << "Ticket Dealer: Thanks!" << endl;
		cout << endl << "Ticket Dealer: I now have " << CountUnsoldTicket << " ticket(s) left."<< endl;
	}
	else if(CountUnsoldTicket <= 0)
	{
		cout << endl << "Sorry, I have no tickets." << endl;
		system("Pause");
		exit(1);
	}
}

void GoToTicketDealer()
{
	if(CountUnsoldTicket <= 0)
	{
		cout << endl << "Ticket Dealer: Sorry, I have no tickets." << endl;
		system("Pause");
		exit(1);
	}
	else if(CountUnsoldTicket > 0)
	{
		if(CountUnsoldTicket == 1)
		{
			cout << endl << "Ticket Dealer: Hello, would you like to buy my last(1) ticket?" << endl;
		}
		else if(CountUnsoldTicket > 1)
		{
			cout << endl << "Ticket Dealer: Hello, would you like to buy some(" << CountUnsoldTicket << ") tickets?" << endl;
		}

		char YesNo = 0;
		cout << "(Please enter either 'Y' for yes or 'N' for no)" << endl;
		cin >> YesNo;
		if(YesNo == 'Y' || YesNo == 'y')
		{
			int CountPurchasingTicket = 0;

			BuyTicket("Ticket Dealer: How many adult ticket do you wish to purchase?",
				CountPurchasingTicket, CountSoldAdultTicket);

			BuyTicket("Ticket Dealer: How many kid ticket do you wish to purchase?", 
				CountPurchasingTicket, CountSoldKidTicket);

			BuyTicket("Ticket Dealer: How many senior ticket do you wish to purchase?", 
				CountPurchasingTicket, CountSoldSeniorTicket);

			BuyTicket("Ticket Dealer: How many family ticket do you wish to purchase?", 
				CountPurchasingTicket, CountSoldFamilyTicket);
		}
		else if(YesNo == 'N' || YesNo == 'n')
		{
			cout << endl << "Okay no problem, have a great day!" << endl;
			exit(0);
		}
	}
}
Comments
We do not like cheting here. Please refrain from doing homework for other people.

Hello, thank you for helping me out. I'm sorry, but when I tried to compile the code it gave me an error on line 64 it said BuyTicket is undeclared (first use this function).

Hello, thank you for helping me out. I'm sorry, but when I tried to compile the code it gave me an error on line 64 it said BuyTicket is undeclared (first use this function).

Good. Ignore what he wrote and do your own work. You learn nothing by having someone do your homework for you.

Follow my post and learn how to be a programmer. Follow Tygawr's post and you'll need him to write your next assignment, too.

Comments
Everyone has their own way of teaching.

Okay, so I did the code. However, I'm stuck with this problem: When the program is finished and the user has purchased tickets it will subtract from the NumberOfTickets. When the program is executed again, it will get the new purchased tickets and subtract with the remaining NumberOfTickets. How can I do this? If someone can just put me on the right track that would be helpful. I managed to this code. Thank you.

void getQuantityTickets(int &AdultQty, int &ChildQty, int &SeniorQty,int &FamilyQty) 
{
    int TotalOfTickets;
    int NumberOfTickets = 55;
    int i = 55; 

    cout<<"Please enter the number of adult tickets you would like:";
    cin >> AdultQty;
    cout<<"Please enter the number of child tickets you would like:";
    cin >> ChildQty;
    cout <<"Please enter the number of senior tickets you would like:";
    cin >>SeniorQty;
    cout <<"Please enter the number of family tickets you would like:";
    cin >>FamilyQty;
    TotalOfTickets = AdultQty+ChildQty+SeniorQty+FamilyQty; 
    NumberOfTickets = NumberOfTickets - TotalOfTickets;
    cout << "Total Qtys " << NumberOfTickets << endl;

    ofstream myfile1;

    myfile1.open ("Tickets Left.txt");

    myfile1 << NumberOfTickets;

    myfile1.close();

    if (! myfile1){
       cout << "File TicketsLeft.txt could not be opened" << endl; 
    }

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.