[prog] Re: C++

Sue Stones suzo at spin.net.au
Wed Jan 21 04:06:27 EST 2004


On Wed, 21 Jan 2004 02:31 am, wolf wrote:
> sorry about that, fixed the initializing of the array...
>
> here's the question from the book to help explain what I'm trying to
> figure out : )
> new code follows after : )
>
> 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"
>
> and now the code : )
>
>
> #include <iostream>
>
> #include <cctype>
>
> using namespace std;
>
> int main()
>
> {
>
> 	int seat [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
You have initialised these positions in the array to contain the numbers 1-10, 
when you really want it to contain all zeros.

> 	int first_class = 0;
>
> 	int economy = 5;
>
> 	char Choice;
>
> 	int i = 0;
>
> 	char option;
>
>
>
>
>
>
>
> 	cout << "Please enter 1 for First Class or enter 2 for Economy: ";
>
> 	cin    >> Choice;
>
> 	cout << endl;
>
> 	if (Choice == 1)
>
>
>
>
> 		switch (Choice)
>
> 		{
>
> 			case '1':
>
> 				cout << "Your choice is 1.";
>
> 				break;

At each place in the switch statement you need to make the changes to the seat 
array, to actually fill the seat.   There is little point in using a switch 
statement just to ecco the users input back to them.  You actually need to 
use it to perform the actions required forthe option chosen.

 You should check here to see if there are any seats available, before making 
the assignment, as well as the code for if there are no seats left.  (think 
about how you would do it in real life).  Don't forget to make sure the 
counter  first_class is pointing to the right place for the next seat to be 
alocated before you break.
>
> 			case '2':
>
> 				cout << "Your choice is 2.";
>
> 				break;
>
> 		//	default:
>
>
> 		}
>
>
>
> 			for ( i = 0; i < 5; i++)
> 			{
> 				cout << "Your assigned seat is: \t" << i << endl;
> 				first_class++;>
> 				if (seat [4] == 1)
> 					cout << endl;
> 				cout << "This section is full. Would you like to be assigned to";
> 				cout << " the economy section?  (Y or N)" << endl;
> 				cout << endl;
> 			}

All of this code is repeated for each seat that you alocate, so the above will 
print 5 statements :
"Your assigned seat is: 0	Your assigned seat is: 1	Your assigned seat is: 2	
Your assigned seat is: 3	Your assigned seat is: 4"


Possibly all or many with "This section is full. Would you like to be assigned 
to the economy section?  (Y or N)"
	
Note your seat numbers do not correspond to the seats in the plane, where is 
the person allocated seat 0 going to sit?

Agin the following code is run at each encty.  In total you are producing 10 
boarding passes for each customer.

> 			for (i = 5; i < 9; i ++)
> 			{
> 				cout << endl;
> 				cout <<" Your assigned seat is: " << i <<  endl;
> 				economy++;
> 				if (seat [9] == 1)
> 					cout << endl;
> 				cout << "This section is full.  Would you like to be assigned to ";
> 				cout << "the economy section? Y or N: " ;
> 				cin >> option;

Note option below, you are testing to see if option contains the ascii 
character \1 which is "!"  so if the answer was Y or N the switch will never 
operate.
> 				if (option == 1)
> 					switch (option)
> 					{
> 						case 'Y':
> 						case 'y':
> 							cout << "Your choice is Y. ";
> 							break;
> 						case 'N':
> 						case 'n':
> 							cout << "Your choice is N. ";
> 							break;
> 						//default:
> 					}
> 			}

According to the question this needs to be printed after the customer has 
rejected the other seat class.  Note you probably want to check that there 
are seats in the other class before you offer them, otherwise you could end 
up in a perpetual loop.

> 			cout << "Next flight leaves in 3 hours." << endl;
> 			return 0;
>
> }

There is more that I could say, but it is well pased my bedtime!

sue


More information about the Programming mailing list