[prog] Re: C++

Sue Stones suzo at spin.net.au
Wed Jan 21 16:28:21 EST 2004


On Wed, 21 Jan 2004 09:57 am, wolf 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

It would make it faster for people to point out your errors if you told us 
what your input and output is.  Otherwise we have to guess you input, and 
calculate the output in our heads.  Then calculate what should have happend.

A switch statement is equivalent to a mulitway if statement, but can be easier 
to read, I thought the original choice of switch was a god one, but it may be 
confusing you.  (it incidentally made it easier to comment on your code 
because I could refer to the switch statement with out confusing things with 
the if statements - but that is beside the point)

I will include comments into your code.  I have removed a lot of the blank 
line, they make it too hard to read.

> #include <iostream>
> using namespace std;
>
> int main()
> {
> 	int section;
> 	int seats[10];
> 	int FC =1; // first class  
	/*  you are now not using the first cell  of the array, so you should at 	
	**	least declare the array to be seats[11]. Though I think it would be much
	**	better to simply change the number priteed out to be one more than the 
	**	array position.
	*/
> 	int EC = 6; // economy
> 	int people;
> 	{
		cout << "Please enter 1 for First Class or enter 2 for Economy: " ;
> 	        cin >> section ;
>
> 		if ( section == 1 ) {
	/* you seem to have confused FC and EC here, remeber FC is the first part of
	**	 the array so you want it to end at 5, ie should be FC <= 5
	*/
> 				if ( FC <= 10 && seats[ FC ] == 0 ) {  
> 					cout <<  "First Class Seat #" + FC << endl;
> 					seats[ FC++ ] = 1;
> 					people++;
> 				}
> 				else if ( FC > 10 && EC <= 5 ) {
> 					cout <<  "First Class is full.  Would Economy be okay?(1 for Yes :
> 2 for No):  "  << endl;
> 					cin >> section;
> 				}
> 				else
> 					cout <<  "Next flight leaves in 3 hours." << endl;
> 		}
> 		else if ( section == 2 ) {
> 				if ( seats[ EC ] == 0 && EC <= 5 ) {
> 					cout <<  "Economy Seat #" + EC << endl;
> 					seats[ EC++ ] = 1;
> 					people++;
> 				}
> 				else if ( EC > 5 && FC <= 10 ) {
> 					cout <<  "Economy is full. Would First Class be okay?(1 for Yes :
> 							2 for No):  " << endl;
> 					cin >> section;
> 				}
> 				else
> 					cout <<  "Next flight leaves in 3 hours.";
> 		}
> 		else
> 				cout <<  "Invalid input.";
> 		}
	/* I don't know where the above two closing brackets are supposed to be
 	** 	 closing
	** I always find it helpfull to label cloding brackets with a comment so that 
	**	I can see what section is being closed. Yours didn't line virtically,
	** 	 which didn't help.  So I would put something like this
	**		}  // if section
	** 	this is a slylistic thing, but I like it.
	**
	**	The sections below are enclosed in braces for no reason, this may not
	** 	 affect how the code compile, but certainly confuses the reader, and make 
	**	debugging difficult.
	*/

	/*
	**	The code below ignore what has happed above, and allocates seats according
	**	 to the initial choice, even if there is no room left in that class, and
	**	 the customer has chosen the other class.  Also it allocates another seat
	**	for each customer.
	**	You should have included the printing of the boarding pass in the code
	**	above (which is still the same error as you made using the switch
	**	statement.
	*/
> 		{
> 			if ( section == 1 ) {
> 				cout <<  "Your seat assignment is " <<  EC << endl ;
> 				seats[ EC++ ] = 1;
> 			}
> 			else {  // section is 2
> 				cout << "Your seat assignment is " <<  FC << endl ;
> 				seats[ FC++ ] = 1;
> 			}
> 			++people;
> 		}
	/*
	**	According to your question this is printed only if the customer is not
	**	 allocated a seat, and so should be included in the if statements above
	**	 that have replaced your qwitch statement.
	*/
> 		 {
> 			cout <<  "Next flight leaves in 3 hours.";>
> 		}
> 	}
>

	/* 	Once you have this woring for ine customer, you will need to enclose it
	**	 in a loop to make it poerational for mutliple customers.  But it may
	**	 help you see what is happening if you do this now rather than wait until
	**	 this part is finished.  
	**	sue
	**/

> A small airline has just purchased a computer for its new automated
> reservations system. You have been asked to program the new system. You
> are to write a program that assigns seats
> on each fight of the airline's only plane which has a capacity of 10
> people.
>
> You program should display the following menu of alternatives - Please
> type 1 for first class and Please type 2 for economy class.  If the
> person types 1 your program should assign
> a seat in the first class section which are seats 1-5. If the person
> types 2 your program
> should assign a seat in the economy section seats 6-10. Your program
> should print
> a boarding pass indicating the person's seat number and whether it is
> in first class or
> economy.
>
> use a single subscripted array to represent the seating chart of the
> plane. Initialize all the elements of the array to 0 to indicate all
> seats are empty. As each seat is assigned set the corresponding
> elements of the array to 1 to indicate that the seat is no longer
> available.
>
> your program should never assign a seat that is already assigned. When
> the first class
> section is full your program should ask the person if it is acceptable
> to be placed in the economy section ( or vice versa) if  yes then make
> the appropriate assignment if not
> print the message "Next flight leaves in 3 hours"
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming



More information about the Programming mailing list