[prog] Re: C++

wolf wolf at wolfrising.net
Tue Jan 20 22:40:42 EST 2004


Hi, here's the latest, I did use a switch again and also followed a 
tutorial
I found online for functions. I would be interested to see the code 
anyone
else came up with as this took me about 2 weeks to get to the point 
where I decided
I better ask for help/suggestions or go crazy : ) My apologies to 
everyone for
taking so long to finally understand why the if statement needed to be 
removed.

Thanks to everyone who responded!! : )

#include <iostream>
#include <cstring>

using namespace std;
void GetASeat ( int [], int, int, char [] );
int minSeatsFIRSTCLASS (0);
int maxSeatsFIRSTCLASS (4);
int minSeatsECONOMY (5);
int maxSeatsECONOMY (9);



int main( void )
{
	
	
	
	int choice = 10;
	int seats[10] = { 0 };
	while(choice != -1)
	{
		
		
		
		
		cout << "Please type 1 for First Class\n";
		cout << "Please type 2 for Economy class\n";
		cout << "Type -1 to quit\n> ";
		cin
			>>
			choice;
		switch
			(choice)
		{
				case 1: GetASeat(seats, minSeatsFIRSTCLASS, maxSeatsFIRSTCLASS,
								 "First Class");
					break;
				case 2: GetASeat(seats, minSeatsECONOMY,
								 maxSeatsECONOMY, "Economy Class");
					break;
				case -1:
						break;
				default : cout << "Input not valid !\n" << endl;
		}
	}
	return
		0;
}
void GetASeat ( int seats[], int MinClass, int MaxClass, char Class[] )
{
	char
	choice;
	for(int i = MinClass; i <= MaxClass; i++)
	{
		if
			(seats[i]!=1)
		{
				seats[i]=1;
				cout << "Seat Number " << (i+1) << " In " << Class <<" has been 
reserved.\n" << endl;
				i = MaxClass + 1;
		}
		if ( i == MaxClass)
		{
			cout << " There are no seats in " << Class<<"\nWould you like to 
switch to another class ? ( y or n)"<< endl;
			cin >> choice;
			switch
				(choice)
			
			{
				
					case 'y':
					case 'Y':
						if (!strcmp("First Class", Class))
							GetASeat(seats, minSeatsECONOMY,
									 maxSeatsECONOMY, "Economy Class");
						else
							GetASeat(seats,
									 minSeatsFIRSTCLASS,
									 maxSeatsFIRSTCLASS, "First Class");
						break;
					case 'n':
					case 'N':
						cout << "Next Airplane leaves in three hours !\n" << endl;
						break;
					default:
							cout << "Invalid answer !\n Reservation Cancelled\n" << endl;
						break;
			}
		}
	}
}

On Jan 20, 2004, at 7:39 PM, Jacinta Richardson wrote:

>
>> here's the newest version - -I think it's getting closer but still
>> doesn't work quite correctly. here's the code, at the bottom of the
>> page I included the sample question I'm working since I changed
>> my strategy away from using switch
>
> How about we step back from the code for a bit and think about the
> solution to the problem...
>
> What variables are we going to need?  I've come up with a few extras 
> for
> you, you may or may not want to consider them.
>
> 	* section - which section the person will travel in
> 	* seats - our list of seats on the airplane
> 	* people - number of people on the plane
>
> 	* FC_start - the starting place for first class seats.  probably 0
> 	* in_FC - how many people are sitting in FC at the moment
> 	* EC_start - the starting place for economy seats.  probably 5
> 	* in_EC - how many people are sitting in EC at the moment
>
> Okay, once we have these variables we need to consider how to execute 
> the
> program.  We probably want some sort of looping structure so that we 
> can
> do this for every passenger.  We then need to determine what section 
> the
> person should go into (based on their wishes as well as our 
> limitations)
> and then issue them with the boarding pass.
>
> -------
> While the plane is not full (people < 10)
>
> 	Ask whether the person wants first class or economy
> 	
> 	If they want first class
>
> 		Check that we actually have seats left in FC
> 		(in_FC must be less than EC_start)
>
> 		If we don't have seats left in FC  (since the plane is not
> 		full, we don't need to worry about checking if there are
> 		still seats in economy)
> 			Ask if we can change their section.
>
> 			If they don't want their section changed
> 				Tell them to come back for the next plane
> 				and process the next customer.
> 			Otherwise
> 				Change their section
> 			Endif
> 		Endif
>
> 	Otherwise if they want economy class
>
> 		Check that we actually have seats in economy class
> 		If economy is full
> 			Ask if we can change their section
>
> 			If they don't want their section changed
> 				Tell them to come back for the next plane
> 				Process the next customer
> 			Otherwise
> 				Change their section
> 			Endif
> 		Endif
> 	Otherwise
> 		They entered invalid data
> 	Endif
>
> 	Now we know that we can put them on the plane so:
> 	Increment the number of people on the plane
>
> 	If they're travelling in first class
> 		Tell them their seat assignment is in_FC+FC_start
> 		Add 1 to number of people in FC (in_FC++)
> 		Add person to plane
> 	Otherwise
> 		Tell them their seat assignment is in_EC+EC_start
> 		Add 1 to number of people in EC
> 		Add person to plane
> 	EndIf
> EndWhile
>
> At this point the plane is full, so note that the next plane leaves in 
> 3
> hours.
> -------
>
> Now there are probably other ways to solve this problem, but I think
> you'll find that you
> 	a) have to use a loop (or write out the same code 10 times)
> 	b) should determine their section first considering limitations
>
> You appear to be attempting b) but not even considering a)
>
> You're also adding people to your plane twice.  That is, you check that
> there are seats left in FC (for example), add the person to the plane 
> and
> then at the end check that if the person is in FC you add the person to
> the plane again.  If you follow my peudocode above you will ONLY add
> people to the plane once you've handled their section.  What is more 
> when
> handling their section you'll ONLY do something if that section is 
> full.
>
> I believe you'll find it very difficult to change the execution flow 
> of my
> suggestion to allow actual seat assignments in the section handling.  
> This
> is because if they change their section you have no good way to handle
> that without duplicating even more code.
>
> I hope this helps.
>
> I have a working example from this pseduocode, so once you've got your 
> own
> working, let me know if you want to see how I handled it.
>
> All the very best,
>
> 	Jacinta
>
>
> --
>    ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
>     `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
>     (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001         |
>   _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
> (il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |
>



More information about the Programming mailing list