RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 5169 | Replies: 25
Reply
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

airplane seating prices

  #1  
Jul 26th, 2005
hi, I'm working on a project and I'm almost done. However, I'm having a few problems with the prices.
this is the guide for the project:
Airplane Seat Allocation System
Write a C++ program for the following task using modular approach.
a) 48 seats in 12 rows with a Aisle in the center. Seats at end of each row is a “Window Seat�. The middle two seats in a row are “Aisle� seats and 4 Seats in row one are “Leg Room� Seats.

b) The program should display the seat plan.

c) Display a welcome greeting ,

d) Menu driven program to allow request for, ‘Window’, ‘Aisle’ and ‘Leg Room’ Seats, display seat # and block (if available). If the seats are not available in existing category, display a sorry message.

e) Provide a ‘Quit’ facility to exit from the program.

AND THIS IS MY CODE SO FAR:

//Airplanes Seating Allocation System

//libraries included
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>

char airplane [12] [4];
float ticketPrice [12] = { 0.0 };
float totalsales = 0.0f;
int row, seat;

//Function Prototypes
void DisplaySeats (void);
void DisplayPrices (float []);
void DisplaySales ();
void PurchaseTicket (float[]);

void main ()
{
	int choice;
	char response = 'Y';

	cout << "WELCOME TO C++ AIRPLANES SEATING ALLOCATION SYSTEM!\n";
	cout <<	"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";

	//initialize airplane with labels (A, S, W, L) - all seats available
	for (int y =0; y < 1; y++)			
	for (int q = 0; q <4; q++)		
		airplane [y][q] = 'L';		//Leg Room Seats
	for (y = 1; y < 12; y++)
	{
		for (int z = 0; z < 4; z++)
		{
			for (int x = 0; x < 1; x++)
				airplane [y][x] = 'S';		//Regular Seats
			for (int v = 1; v < 3; v++)
				airplane [y][v] = 'A';		//Aisle Seats
			for (int w = 3; w <4; w++)
				airplane [y][w] = 'W';		//Window Seats
		}
	}

	//initialize seat prices for each of the 12 rows
	for (int i = 0; i < 12; i++)
	{
		cout << "\nPlease enter ticket price for Row " << i + 1 << ": ";
		cin >> ticketPrice [i];
	}

while (response = 'Y')
{
	//display menu of choices

	cout << "\n\n\n\tC++ Airplane Seating Allocation System" << endl  <<  endl;
	cout << "\n\t1.   View Availabe Seats";
	cout << "\n\t2.   View Seating Prices";
	cout << "\n\t3.   View Ticket Sales";
	cout << "\n\t4.   Purchase a Ticket";
	cout << "\n\t5.   Exit the Program\n\n";
	cout << "\n\tEnter your choice (1-5): ";
	cin >> choice;
	switch (choice)
	{
	case 1:
		cout << "Testing. DisplaySeats() called\n";
		DisplaySeats();
		break;
	case 2:
		DisplayPrices(ticketPrice);
		break;
	case 3:
		DisplaySales();
		break;
	case 4:
		PurchaseTicket(ticketPrice);
		break;
	case 5: 
		exit (0);
		break;
	default:
		exit(0);
		break;
	} //end switch
}; //end while
} //end main

//Display a seating chart of the airplane
void DisplaySeats (void)
{
	cout << "\n\tSEATS\n";
	cout << "	1234";
	cout << endl;
	
	for (int r = 0; r < 12; r++)
	{
		cout << "\nRow " << setw(2) << r + 1 << "\t";
			for (int s =0; s < 4; s++)
			{
				cout << airplane[r][s];
			}
	}
cout << "\n\n\n\tLegend:\t* = Sold";
cout << "\n\t\tA = Aisle Seats (Available)";
cout << "\n\t\tW = Window Seats (Available)";
cout << "\n\t\tS = Regular Seats (Avaiable)";
cout << "\n\t\tL = Leg Room Seats (Available)";
cout << endl;

char keyin;
cout << endl << endl;
cout << "\nPress an alphanumeric key to continue.";
cin >> keyin;

} //end DisplaySeats

void DisplayPrices(float price[])
{
	cout << "\n\tTicket Prices By Row:  " << endl << endl;
	cout << "\tRow			Price" << endl;
	cout << "\t------			-------" << endl << endl;
	cout.precision(2);
	cout.setf(ios::fixed | ios::right);
	for (int m =0; m < 12; m++)
	{
		cout << "\n\tRow " << setw(2) << m+1 << ":\t\t" << setw(5) << price[m];
	}

char keyin;
cout << endl << endl;
cout << "\nPress an alphanumeric key to continue.";
cin >> keyin;
}

void PurchaseTicket(float price[])
{
	int a, b = 0;
	char response;

	cout << "\n\t		 	C++ Airplane Seating Allocation System" << endl;
	cout << "\t\tTicket Purchase Opportunity" << endl;
do
{
	do
	{
	cout << "\nPlease enter row number (1-12): ";
	cin >> a;
	} while (a <1 || a > 12);
	do
	{
	cout << "\nPlease enter seat number (1-4): ";
	cin >> b;
	} while ( b < 1  || b > 4);

	if (airplane [a-1][b-1] == 'A' || airplane [a-1][b-1] == 'L' || 
		airplane [a-1][b-1] == 'W' || airplane [a-1][b-1] == 'S')		//if the seat is available
	{
	airplane[a-1][b-1] = '*';		//indicate that the seat is taken
	totalsales +=ticketPrice[a-1];
	cout << "\nYour purchase of Seat " << b << "in Row " << a << "is confirmed." << endl;
	}
	else 		//seat has bee sold
	{
		cout << "\nSorry. That seat has been sold.\n";
	}

	cout << "\nWould you like to purchase another seat?";
	cin >> response;
} while (toupper(response) == 'Y');
}

void DisplaySales ()
{
	cout.setf(ios::fixed);
	cout << "\n\nTotal Sales to Date:  $" << setprecision(2) << totalsales << endl;
}

I tried to set different prices for the seats but I don't know how to work around the array problem. what I mean is that, how can I put these different prices in dirrent arrays and have them added up and stored inthe totalsales function?

I also don't know how to separate the column so that it would have an aisle down the middle.

Can you please help?

Thanks in advance,
Karen
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: airplane seating prices

  #2  
Jul 26th, 2005
For displaying the aisle, you can either write a string with spaces in it, " "
or a tab character, "\t" in your seat display couts.

Edit: I'll be back later. Gotta catch a bus.
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: airplane seating prices

  #3  
Jul 26th, 2005
Originally Posted by Drowzee
For displaying the aisle, you can either write a string with spaces in it, " "
or a tab character, "\t" in your seat display couts.

Edit: I'll be back later. Gotta catch a bus.

I know this function, however, I put my seats in a 2-D array, how do I put the "\t" function in a 2-D array?
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: airplane seating prices

  #4  
Jul 26th, 2005
hi,
just in case I didn't sound very clear with the original post, I'd like different prices for different types of seatings (i.e aisle seats would cost $5, window seats $10) I only did the row prices because I don't know how to do the types prices......I'm rambling... Ok what I WANT is that I want each type of seating has its own price.

Also the prices can't be preset, the program has to asks the user to enter their prices. So, instead of asking the user to "enter the price for Row 1 (or 2, 3....12), it would ask the user to "enter the price for <W>indow seats, <A>isle seats....
that's why I'm thinking that I have to save this in an array or a function so that the total can be calculated. And This PART I don't know how to do, because it links with other functions.

Did I make myself clear to you? Sorry, I'm not very well with words.

Can you please help?

Thanks,
Karen
Reply With Quote  
Join Date: Jul 2005
Location: London
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: airplane seating prices

  #5  
Jul 26th, 2005
something like....
#include <iostream>
#include <limits>
using namespace std;

int GetInput(string s)
{
   cout<<s;
   int input;
   while (!(cin>>input))
   {
      cerr<<endl<<"error! re-enter"<<endl;
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(),'\n');
   }
   return input;
}

int main()
{
   int windowseatprice = GetInput(string("How much for a window seat"));
   return 0;
}
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: airplane seating prices

  #6  
Jul 26th, 2005
Originally Posted by Stoned_coder
something like....
#include <iostream>
#include <limits>
using namespace std;

int GetInput(string s)
{
   cout<<s;
   int input;
   while (!(cin>>input))
   {
      cerr<<endl<<"error! re-enter"<<endl;
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(),'\n');
   }
   return input;
}

int main()
{
   int windowseatprice = GetInput(string("How much for a window seat"));
   return 0;
}

Ummm...hi....I forgot to mention that I'm a level-1 C++ beginner.
your code looks really complicated to me. I didn't learn about #include <limits> yet, nor cerr, nor...this "cin.ignore(numeric_limits<streamsize>::max(),'\n');"

do you know a less-advanced way of doing this?

sorry,
karen
Reply With Quote  
Join Date: Jul 2005
Location: London
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: airplane seating prices

  #7  
Jul 26th, 2005
OK, I'll explain it a little. GetInput takes a string to print to the console and returns an int that was entered. If there is a bad input then an error message is printed and the garbage removed from the stream so we can try recieve input again. When for instance cin is expecting an int but gets a char it goes into a failed state. All operations on a failed stream are null and void so first we must clear the error flags. This is what the cin.clear does. After that we can proceed to remove any garbage entered. The cin.ignore line basically says ignore as many chars as you can from the stream or up to the next newline char.This lets us proceed to getting an int from the user. Back in main we declare an int and initialise it with the return value of GetInput.
Need any further elaboration?
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: airplane seating prices

  #8  
Jul 26th, 2005
Hi,
while waiting for help, I help myself a bit (LOL that sounds so akward). ANYWAY.

Instead of the original code, which is to ask for the price of 12 rows:

//initialize seat prices for each of the 12 rows
for (int i = 0; i < 12; i++)
{
cout << "\nPlease enter ticket price for Row " << i + 1 << ": ";
cin >> ticketPrice [i];
}

I changed it to this:

void main ()
{
	int choice;
	char response = 'Y';

	float seatprices[4] = { 4.0, 5.0, 6.0, 7.0 }; //just some defaults here: ASWL
	char seattypes[4][12] = { "Aisle", "Regular", "Window", "Leg Room" }; //this makes it possible to do input with a loop


	cout << "WELCOME TO C++ AIRPLANES SEATING ALLOCATION SYSTEM!\n";
	cout <<	"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";

	//initialize airplane with labels (A, S, W, L) - all seats available
	for (int y =0; y < 1; y++)			
	for (int q = 0; q <4; q++)		
		airplane [y][q] = 'L';		//Leg Room Seats
	for (y = 1; y < 12; y++)
	{
		for (int z = 0; z < 4; z++)
		{
			for (int x = 0; x < 1; x++)
				airplane [y][x] = 'S';		//Regular Seats
			for (int v = 1; v < 3; v++)
				airplane [y][v] = 'A';		//Aisle Seats
			for (int w = 3; w <4; w++)
				airplane [y][w] = 'W';		//Window Seats
		}
	}

	//seatypes inputs
	for (int i = 0; i < 4; i++)
	{
	cout << "Please enter price for " << seattypes[i] << " seats: ";
	cin >> seatprices[i];
	cout << endl;
	}

and now I have to find a new way to display this in the DisplayPrices () function, and I don't think I did it right.

void DisplayPrices(float seatprices[], float seattypes[])
{
	cout.precision(2);
	cout.setf(ios::fixed | ios::right);
	cout << "\n\tTicket Prices By Seat Types:  " << endl << endl;
	cout << "\tSeat Types			Price" << endl;
	cout << "\t------			-------" << endl << endl;
	for (int m = 0; m < 4; m++)
		seattypes[m];
	for (int n = 0; n < 4; n++)
		seatprices[n];
	
char keyin;
cout << endl << endl;
cout << "\nPress an alphanumeric key to continue.";
cin >> keyin;
}

can you tell me what I did wrong in the code above?

Karen
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: airplane seating prices

  #9  
Jul 26th, 2005
Originally Posted by Stoned_coder
OK, I'll explain it a little. GetInput takes a string to print to the console and returns an int that was entered. If there is a bad input then an error message is printed and the garbage removed from the stream so we can try recieve input again. When for instance cin is expecting an int but gets a char it goes into a failed state. All operations on a failed stream are null and void so first we must clear the error flags. This is what the cin.clear does. After that we can proceed to remove any garbage entered. The cin.ignore line basically says ignore as many chars as you can from the stream or up to the next newline char.This lets us proceed to getting an int from the user. Back in main we declare an int and initialise it with the return value of GetInput.
Need any further elaboration?

wow, that was certainly a lot of information in one paragraph! I got to digest all that slowly (i'm a bit slow, sorry). I think I understand what you coded...but I'm not really familiar with it...and my project is due in 2 days...so it's safer to stick to what I know right now....But your information is certainly great for future reference though (i'm sure more projects are coming my way...sighnnn).

anyway, I just posted my new code. Can you check that out and give me suggestions?
Reply With Quote  
Join Date: Jul 2005
Location: London
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: airplane seating prices

  #10  
Jul 26th, 2005
btw. add #include<string> to top of that code. Forgot that.

Your code is incomplete. If you post a complete example I can look over in my compiler then i'll take a look. I would urge you to at least check out the string type rather than use char arrays. Always remember main never returns void. It always returns int.Your loops look messy.Stick to the KISS principle and you wont go far wrong.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:40 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC