944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1101
  • C++ RSS
Nov 11th, 2007
0

Arrays

Expand Post »
ok my teacher wants me to write a program using these instructions :

A small airline has just purshased a computer for its new automated reserations system. You have been asked to program the new system. You are to write a program to assign seats on each flight of the airline's only plane (capacity: 10 seats). Your program should display the following menu alternatives--Please type 1 for "First Class" and PLease type 2 for "Economy". If the persons types 1, YOur program should assign a seat in the first class section (seat 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 the first class or economy section of the plane.
Use one-dimensional array to represent the seating chart of the plane. Initialize all the elements of the array to 0 to indicate that 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, of course, never assign a seat that has already been 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(and viceversa). If yes, then make the appropiate seat assignment. If no, the print the message "Next flight leaes in 3 hours".

this is what i have so far and i need to know what can i do assign each section a seat, but individually, i don't want it to print all together, i just want to ask do yo want first class or economy depending on the answer i assign the corresponding seat can someone help me pleaseee what is im doing wrong


//Airplane Reservation System

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;


int main()
{
int firstClass = 0;//first class section
int economy = 0;//economy section
int sections= 0;//to choose the section of the seats
int total = 0;
int sectionsCounter = 0;
//use an array for the capacity of 10 seats
int seats[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

//Menu Alternatives



cout<<"Please type 1 for ""First Class"":"<<"\n"<<endl;
cout<<"Please type 2 for ""Economy"":"<<endl;
cin>>sections;



//use an if statement to assign a seat in first class
if ( sections == 1)
{
sections = 1;
cout<<"First Class"<<setw (13)<<"Seats"<<endl;

for ( int i = 0; i <= 4; i++)

cout<<setw(7)<<sections<<setw (13)<<seats[i]<<endl;
}//end of if

//use a if statement to assign a seat in economy section
else
if ( sections == 2)
{
sections = 1;
cout<<"Economy"<<setw (13)<<"Seats"<<endl;
for ( int i = 5; i < 10; i++)

cout<<setw(7)<<sections<<setw (13)<<seats[i]<<endl;
}//end of if
system("pause");
return 0;
}//end of main
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
neilyan is offline Offline
5 posts
since Sep 2007
Nov 12th, 2007
0

Re: Arrays

You mean something like this:
C++ Syntax (Toggle Plain Text)
  1. if ( sections == 1) //first class reqeusted
  2. {
  3. if(firstClass < 4)//implies there's only 4 first class seats available on this flight
  4. ++firstClass; //assign a seat in first class
  5. }
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 12th, 2007
0

Re: Arrays

no, what i mean is that the program should print First Class or Economy 0 until the user inputs the number of section, and when the user choose wich section they want, the program should assign a seat for each section, if it is first class it has to be in the seats from 1-5 and if it is economy it has to be in the seats from 6-10
Reputation Points: 10
Solved Threads: 0
Newbie Poster
neilyan is offline Offline
5 posts
since Sep 2007
Nov 12th, 2007
0

Re: Arrays

If you want it to print out 0's then set the array to 0's

C++ Syntax (Toggle Plain Text)
  1. int seats[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
with this you're setting each array with it's respective number.

also as a heads up if you're looking for this to loop until all the seats are full you need to set a lop before the menu starts to go back to the menu once the user is done making his selections.

C++ Syntax (Toggle Plain Text)
  1. //use an if statement to assign a seat in first class
  2. if ( sections == 1)
  3. {
  4. sections = 1;
  5. cout<<"First Class"<<setw (13)<<"Seats"<<endl;
  6.  
  7. for ( int i = 0; i <= 4; i++)
  8.  
  9. cout<<setw(7)<<sections<<setw (13)<<seats[i]<<endl;
  10. }//end of if

with this you're only printing out the numbers between seats[0] to seats[4]. first thing first take out the sections = 1; its doing nothing. Then find a way to change seats[] to the value 1 once they selected either 1 or 2 for their seating. I would do something like
int i =0;
while (i < 4 && seats[i] ==1)
i++;
seats[i] = 1;

Hope this helps out.
Reputation Points: 21
Solved Threads: 1
Light Poster
helixkod is offline Offline
49 posts
since Oct 2007
Nov 12th, 2007
0

Re: Arrays

Use firstClass and and economy to calculate indexes into the array of seats which represent the first available seat in each section. Here's something that might guide you.
C++ Syntax (Toggle Plain Text)
  1. if(sections == 1) //customer wants first class
  2. {
  3. if(firstClass < 5)/*there's only 5 seats in first class, they are seats[0], seats[1], seats[2], seats[3], and seats[4]*/
  4. cout << "you will be assigned seat " << seats[firstClass];
  5. /*now there's one less seat class in first class
  6. so increment firstClass so you don't give the seat out twice*/
  7. ++firstClass;
  8. }
  9. if(sections == 2) //they want economy.
  10. /*the first open seat in economy will be at index i == 5 + economy. You can write the code notifying the customer what seat they were assigned and keeping track of the next available seat in economy */
Last edited by Lerner; Nov 12th, 2007 at 2:58 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 14th, 2007
0

Re: Arrays

Thanks for all your help it was very helpful
Reputation Points: 10
Solved Threads: 0
Newbie Poster
neilyan is offline Offline
5 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: random number generation
Next Thread in C++ Forum Timeline: C++ - possible c_str problem???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC