943,546 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7231
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 26th, 2005
0

airplane seating prices

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1.  
  2. //Airplanes Seating Allocation System
  3.  
  4. //libraries included
  5. #include <iostream.h>
  6. #include <stdlib.h>
  7. #include <iomanip.h>
  8.  
  9. char airplane [12] [4];
  10. float ticketPrice [12] = { 0.0 };
  11. float totalsales = 0.0f;
  12. int row, seat;
  13.  
  14. //Function Prototypes
  15. void DisplaySeats (void);
  16. void DisplayPrices (float []);
  17. void DisplaySales ();
  18. void PurchaseTicket (float[]);
  19.  
  20. void main ()
  21. {
  22. int choice;
  23. char response = 'Y';
  24.  
  25. cout << "WELCOME TO C++ AIRPLANES SEATING ALLOCATION SYSTEM!\n";
  26. cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
  27.  
  28. //initialize airplane with labels (A, S, W, L) - all seats available
  29. for (int y =0; y < 1; y++)
  30. for (int q = 0; q <4; q++)
  31. airplane [y][q] = 'L'; //Leg Room Seats
  32. for (y = 1; y < 12; y++)
  33. {
  34. for (int z = 0; z < 4; z++)
  35. {
  36. for (int x = 0; x < 1; x++)
  37. airplane [y][x] = 'S'; //Regular Seats
  38. for (int v = 1; v < 3; v++)
  39. airplane [y][v] = 'A'; //Aisle Seats
  40. for (int w = 3; w <4; w++)
  41. airplane [y][w] = 'W'; //Window Seats
  42. }
  43. }
  44.  
  45. //initialize seat prices for each of the 12 rows
  46. for (int i = 0; i < 12; i++)
  47. {
  48. cout << "\nPlease enter ticket price for Row " << i + 1 << ": ";
  49. cin >> ticketPrice [i];
  50. }
  51.  
  52. while (response = 'Y')
  53. {
  54. //display menu of choices
  55.  
  56. cout << "\n\n\n\tC++ Airplane Seating Allocation System" << endl << endl;
  57. cout << "\n\t1. View Availabe Seats";
  58. cout << "\n\t2. View Seating Prices";
  59. cout << "\n\t3. View Ticket Sales";
  60. cout << "\n\t4. Purchase a Ticket";
  61. cout << "\n\t5. Exit the Program\n\n";
  62. cout << "\n\tEnter your choice (1-5): ";
  63. cin >> choice;
  64. switch (choice)
  65. {
  66. case 1:
  67. cout << "Testing. DisplaySeats() called\n";
  68. DisplaySeats();
  69. break;
  70. case 2:
  71. DisplayPrices(ticketPrice);
  72. break;
  73. case 3:
  74. DisplaySales();
  75. break;
  76. case 4:
  77. PurchaseTicket(ticketPrice);
  78. break;
  79. case 5:
  80. exit (0);
  81. break;
  82. default:
  83. exit(0);
  84. break;
  85. } //end switch
  86. }; //end while
  87. } //end main
  88.  
  89. //Display a seating chart of the airplane
  90. void DisplaySeats (void)
  91. {
  92. cout << "\n\tSEATS\n";
  93. cout << " 1234";
  94. cout << endl;
  95.  
  96. for (int r = 0; r < 12; r++)
  97. {
  98. cout << "\nRow " << setw(2) << r + 1 << "\t";
  99. for (int s =0; s < 4; s++)
  100. {
  101. cout << airplane[r][s];
  102. }
  103. }
  104. cout << "\n\n\n\tLegend:\t* = Sold";
  105. cout << "\n\t\tA = Aisle Seats (Available)";
  106. cout << "\n\t\tW = Window Seats (Available)";
  107. cout << "\n\t\tS = Regular Seats (Avaiable)";
  108. cout << "\n\t\tL = Leg Room Seats (Available)";
  109. cout << endl;
  110.  
  111. char keyin;
  112. cout << endl << endl;
  113. cout << "\nPress an alphanumeric key to continue.";
  114. cin >> keyin;
  115.  
  116. } //end DisplaySeats
  117.  
  118. void DisplayPrices(float price[])
  119. {
  120. cout << "\n\tTicket Prices By Row: " << endl << endl;
  121. cout << "\tRow Price" << endl;
  122. cout << "\t------ -------" << endl << endl;
  123. cout.precision(2);
  124. cout.setf(ios::fixed | ios::right);
  125. for (int m =0; m < 12; m++)
  126. {
  127. cout << "\n\tRow " << setw(2) << m+1 << ":\t\t" << setw(5) << price[m];
  128. }
  129.  
  130. char keyin;
  131. cout << endl << endl;
  132. cout << "\nPress an alphanumeric key to continue.";
  133. cin >> keyin;
  134. }
  135.  
  136. void PurchaseTicket(float price[])
  137. {
  138. int a, b = 0;
  139. char response;
  140.  
  141. cout << "\n\t C++ Airplane Seating Allocation System" << endl;
  142. cout << "\t\tTicket Purchase Opportunity" << endl;
  143. do
  144. {
  145. do
  146. {
  147. cout << "\nPlease enter row number (1-12): ";
  148. cin >> a;
  149. } while (a <1 || a > 12);
  150. do
  151. {
  152. cout << "\nPlease enter seat number (1-4): ";
  153. cin >> b;
  154. } while ( b < 1 || b > 4);
  155.  
  156. if (airplane [a-1][b-1] == 'A' || airplane [a-1][b-1] == 'L' ||
  157. airplane [a-1][b-1] == 'W' || airplane [a-1][b-1] == 'S') //if the seat is available
  158. {
  159. airplane[a-1][b-1] = '*'; //indicate that the seat is taken
  160. totalsales +=ticketPrice[a-1];
  161. cout << "\nYour purchase of Seat " << b << "in Row " << a << "is confirmed." << endl;
  162. }
  163. else //seat has bee sold
  164. {
  165. cout << "\nSorry. That seat has been sold.\n";
  166. }
  167.  
  168. cout << "\nWould you like to purchase another seat?";
  169. cin >> response;
  170. } while (toupper(response) == 'Y');
  171. }
  172.  
  173. void DisplaySales ()
  174. {
  175. cout.setf(ios::fixed);
  176. cout << "\n\nTotal Sales to Date: $" << setprecision(2) << totalsales << endl;
  177. }

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
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 26th, 2005
0

Re: airplane seating prices

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.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 26th, 2005
0

Re: airplane seating prices

Quote 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?
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 26th, 2005
0

Re: airplane seating prices

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
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 26th, 2005
0

Re: airplane seating prices

something like....
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <limits>
  3. using namespace std;
  4.  
  5. int GetInput(string s)
  6. {
  7. cout<<s;
  8. int input;
  9. while (!(cin>>input))
  10. {
  11. cerr<<endl<<"error! re-enter"<<endl;
  12. cin.clear();
  13. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  14. }
  15. return input;
  16. }
  17.  
  18. int main()
  19. {
  20. int windowseatprice = GetInput(string("How much for a window seat"));
  21. return 0;
  22. }
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Jul 26th, 2005
0

Re: airplane seating prices

Quote originally posted by Stoned_coder ...
something like....
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <limits>
  3. using namespace std;
  4.  
  5. int GetInput(string s)
  6. {
  7. cout<<s;
  8. int input;
  9. while (!(cin>>input))
  10. {
  11. cerr<<endl<<"error! re-enter"<<endl;
  12. cin.clear();
  13. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  14. }
  15. return input;
  16. }
  17.  
  18. int main()
  19. {
  20. int windowseatprice = GetInput(string("How much for a window seat"));
  21. return 0;
  22. }
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
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 26th, 2005
0

Re: airplane seating prices

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?
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Jul 26th, 2005
0

Re: airplane seating prices

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:

C++ Syntax (Toggle Plain Text)
  1.  
  2. void main ()
  3. {
  4. int choice;
  5. char response = 'Y';
  6.  
  7. float seatprices[4] = { 4.0, 5.0, 6.0, 7.0 }; //just some defaults here: ASWL
  8. char seattypes[4][12] = { "Aisle", "Regular", "Window", "Leg Room" }; //this makes it possible to do input with a loop
  9.  
  10.  
  11. cout << "WELCOME TO C++ AIRPLANES SEATING ALLOCATION SYSTEM!\n";
  12. cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
  13.  
  14. //initialize airplane with labels (A, S, W, L) - all seats available
  15. for (int y =0; y < 1; y++)
  16. for (int q = 0; q <4; q++)
  17. airplane [y][q] = 'L'; //Leg Room Seats
  18. for (y = 1; y < 12; y++)
  19. {
  20. for (int z = 0; z < 4; z++)
  21. {
  22. for (int x = 0; x < 1; x++)
  23. airplane [y][x] = 'S'; //Regular Seats
  24. for (int v = 1; v < 3; v++)
  25. airplane [y][v] = 'A'; //Aisle Seats
  26. for (int w = 3; w <4; w++)
  27. airplane [y][w] = 'W'; //Window Seats
  28. }
  29. }
  30.  
  31. //seatypes inputs
  32. for (int i = 0; i < 4; i++)
  33. {
  34. cout << "Please enter price for " << seattypes[i] << " seats: ";
  35. cin >> seatprices[i];
  36. cout << endl;
  37. }

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.

C++ Syntax (Toggle Plain Text)
  1. void DisplayPrices(float seatprices[], float seattypes[])
  2. {
  3. cout.precision(2);
  4. cout.setf(ios::fixed | ios::right);
  5. cout << "\n\tTicket Prices By Seat Types: " << endl << endl;
  6. cout << "\tSeat Types Price" << endl;
  7. cout << "\t------ -------" << endl << endl;
  8. for (int m = 0; m < 4; m++)
  9. seattypes[m];
  10. for (int n = 0; n < 4; n++)
  11. seatprices[n];
  12.  
  13. char keyin;
  14. cout << endl << endl;
  15. cout << "\nPress an alphanumeric key to continue.";
  16. cin >> keyin;
  17. }

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

Karen
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 26th, 2005
0

Re: airplane seating prices

Quote 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?
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 26th, 2005
0

Re: airplane seating prices

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.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005

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: Image manipulations(newbie)
Next Thread in C++ Forum Timeline: Need hellp with my array





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


Follow us on Twitter


© 2011 DaniWeb® LLC