943,567 Members | Top Members by Rank

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

Re: airplane seating prices

Hi, This is my complete code:

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

yeah, I'm aware that's it's a bit (ok, a lot) messy. But since I'm a beginner, I don't know how to take any other shortcuts.

Can you take a look at the DisplayPrices() and the PurchaseTickets() functions? I post my questions there as well.

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

Re: airplane seating prices

I looked at your code. It really could benefit from a rewrite. I got this far then had to go out so rest left as exercise for you. see how you do. Post later what you come up with and ill look it over.
C++ Syntax (Toggle Plain Text)
  1. // You have shown enough effort for me to help you rewrite this
  2. // We will use standard headers and not the deprecated ones you were using.
  3. // I am assuming you are running under a windows OS so we can use a few
  4. // facilities provided by the OS to do things like clearing the screen.
  5.  
  6. #include<iostream>
  7. #include<string>
  8. #include<limits>
  9. #include<windows.h>
  10. // windows.h defines a max macro that will make our life hell so we undef it
  11. #undef max
  12.  
  13. // the functions and objects in the standard headers are all declared within namespace
  14. // std. We will bring them all into the global scope so we dont have to prefix everything
  15. // with std:: with this simple line.
  16. using namespace std;
  17.  
  18. // here is how to clear the screen on a windows console. You do not need to
  19. // necessarily understand this to use it.Suffice to say that a call to this
  20. // will clear the screen and reset the cursor pos to the top left corner.
  21. void clrscr()
  22. {
  23. COORD coordScreen = { 0, 0 };
  24. DWORD cCharsWritten;
  25. CONSOLE_SCREEN_BUFFER_INFO csbi;
  26. DWORD dwConSize;
  27. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  28. GetConsoleScreenBufferInfo(hConsole, &csbi);
  29. dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  30. FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
  31. GetConsoleScreenBufferInfo(hConsole, &csbi);
  32. FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
  33. SetConsoleCursorPosition(hConsole, coordScreen);
  34. }
  35.  
  36. // Lets introduce a struct to represent our seats. A struct is an aggregate type made up
  37. // of smaller types.
  38. // You declare one like so :-
  39. // struct STRUCTNAME { members };
  40. // you make an object of your struct like declaring an int.
  41. // STRUCTNAME obj;
  42. // You access the members with the dot operator like so...
  43. // struct mystruct{inta;intb;};
  44. // mystruct astruct;
  45. // astruct.a = 0;astruct.b = 0;
  46. struct Seat
  47. {
  48. string seattype;
  49. float seatprice;
  50. int row;
  51. int seatnumber;
  52. bool taken; // a bool has 2 states true and false. When this is true the seat is taken
  53. };
  54.  
  55. // Now we will make a global array of seats to represent the plane.
  56. // We will use this all over the place so global as a convenience.
  57. // There are 12 rows of 4 seats so...
  58. Seat seatmap[12][4];
  59.  
  60. // Lets write a function to get the prices of seats
  61. float GetInput(string s)
  62. {
  63. cout<<s;
  64. float input;
  65. while (!(cin>>input))
  66. {
  67. cerr<<endl<<"error! re-enter"<<endl;
  68. cin.clear();
  69. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  70. }
  71. return input;
  72. }
  73.  
  74. // armed with that we can write a function to initalise the seatmap
  75. void InitSeatMap()
  76. {
  77. float Legroomprice = GetInput(string("Enter cost of a leg-room seat ? "));
  78. float Windowprice = GetInput(string("Enter cost of a window seat ? "));
  79. float Aisleprice = GetInput(string("Enter cost of an aisle seat ? "));
  80. for (int i=0;i<12;++i)
  81. for(int j=0;j<4;++j)
  82. {
  83. seatmap[i][j].row = i+1;
  84. seatmap[i][j].seatnumber = j+1;
  85. seatmap[i][j].taken = false; // no seat is taken yet
  86. if (i==0) // legroom seats
  87. {
  88. seatmap[i][j].seattype = "legroom";
  89. seatmap[i][j].seatprice = Legroomprice;
  90. }
  91. if((i != 0) && (j==0 || j==3)) // we have a windowseat
  92. {
  93. seatmap[i][j].seattype = "window";
  94. seatmap[i][j].seatprice = Windowprice;
  95. }
  96. if((i != 0) && (j==1 || j==2)) // we have an aisle seat
  97. {
  98. seatmap[i][j].seattype = "aisle";
  99. seatmap[i][j].seatprice = Aisleprice;
  100. }
  101. }
  102. }
  103.  
  104. // a quick function to output a sorry message
  105. void Sorry()
  106. {
  107. cout<<endl<<"Sorry but that type of seat is not available!!!"<<endl;
  108. }
  109.  
  110. // now we can write a function to allocate a seat
  111. void AllocSeat(string type)
  112. {
  113. // left as exercise to reader
  114. // you can check strings for equality like
  115. // if (type == "legroom")
  116. }
  117.  
  118. void Menu()
  119. {
  120. clrscr();
  121. // exercise for reader
  122. }
  123.  
  124. void DisplaySeatMap()
  125. {
  126. clrscr();
  127. for (int i=0;i<12;++i)
  128. for (int j=0;j<4;++j)
  129. {
  130. if (seatmap[i][j].seattype == "legroom")
  131. {
  132. cout << ((seatmap[i][j].taken) ? "*\t" : "L\t");
  133. }
  134. if (seatmap[i][j].seattype == "window")
  135. {
  136. cout << ((seatmap[i][j].taken) ? "*\t" : "W\t");
  137. }
  138. if (seatmap[i][j].seattype == "aisle")
  139. {
  140. cout << ((seatmap[i][j].taken) ? "*\t" : "A\t");
  141. }
  142. if (j==3)
  143. cout<<endl;
  144. }
  145. }
  146.  
  147.  
  148.  
  149.  
  150. int main()
  151. {
  152. InitSeatMap();
  153. Menu();
  154.  
  155. // exercise for reader
  156.  
  157. return 0;
  158. }
You will still need to write the bits I left as an exercise and also add any more functions you deem necessary. Its reasonably simple stuff but some of it you may not have come across yet. Try follow whats been done so far. It should be reasonably easy for you to finish it off.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Jul 27th, 2005
0

Re: airplane seating prices

OMG! You're asking me to rewrite my code, which I've spent days (over a week already) doing. Please don't be that cruel. i'm only a beginner and I've slaved over this project so much I can't bear to start over again.
I looked over the code that you're written so far...and to be honest...I can follow it but I can't do that on my own...I have no idea what to do next!
Can you please just help me with my code? with the parts where I got stuck on? Please?
Right now, I've rewritten parts of it. and there is one mistake that I should mention. There are only 3 types of seats: leg room, Window, and aisle seats. There are no "regular seats" because airplanes have windows on both sides *duh*.

My modification: the DisplaySeats() and the DisplayPrices() functions.

My Problem: the PurchaseTicket() and the DisplaySales() functions. Since each type of seat has different prices, I've trying to ask for the user to enter the seattype and then the row and the seat numbers. I'm only able to code the program to ask for the row and seat numbers.

Can you tell me what's wrong with my DisplayPrices() function? Why doesn't display anything when I tested it?


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

I'm so sorry I'm not any brighter at this. It's really hard to learn this stuff.

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

Re: airplane seating prices

You should really try to finish my code. I can help you so dont panic. There was too many errors in your original code to make it worthwhile to fix rather than rewrite. Also your design doesn't lend to code brevity. Notice in mine all the data you need to do any calculations is all stored in an array of structs. I gave in the comments a little info on structs, certainly enough for you to be able to grasp what i had written. Notice how it makes things easy and cuts down in general on the amount of loops you need to write.It enhances code readability and therefore makes it easier for you to understand.If there is any part of my code you dont understand just point it out and ill help. Try first to write the AllocSeat function. You ask for wanted seattype. You can then iterate the array for the first of that seattype whose taken member is also false. Set taken to true or call Sorry() and that ones done. There is a few examples of iterating that array of structs already in the code so that shouldn't phase you. There are also examples of comparing strings and assigning struct members. Most of the code is already written for you. Theres only a few funcs to add and maybe one or two to add a line to.
Dont put yourself down for not being bright. I know quite a few people who describe themselves in that manner and some of those are very good coders. Slow and steady is better than fast and falling flat on your face. C++ is hard to learn, I wont lie to you but its not impossible to grasp the simple constructs I have shown you here. std::strings are a much easier type to work with than char array strings and should be taught before the latter. There is enough information in the code to allow you to use the struct array confidently. loops ifs and function calls are about all you need. If you can follow what I have written then you can finish it. I'll be around for about 4 hrs from time of this post.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Jul 27th, 2005
0

Re: airplane seating prices

OK, I took your advice and gave it a try.

C++ Syntax (Toggle Plain Text)
  1. void AllocSeat(string type)
  2. {
  3.  
  4. int a, b;
  5.  
  6. do
  7. {
  8. cout << "What type of seat would you like?\n";
  9. cin >> type;
  10. if (type == "legroom")
  11. {
  12. do
  13. {
  14. for (int y =0; y < 1; y++)
  15. for (int q = 0; q <4; q++)
  16. cout << "\nPlease enter seat number (1-4): ";
  17. cin >> b;
  18. if (string(b, 'L') == 0)
  19. {
  20. seatmap[y][b] == '*';
  21. }
  22. else
  23. {
  24. Sorry();
  25. }
  26. } while ( b < 1 || b > 4);
  27. }
  28. if (type == "aisle")
  29. {
  30. do
  31. {
  32. for (int z = 0; z < 4; z++)
  33. {
  34. for (int v = 1; v < 3; v++)
  35. {
  36. cout << "\nPlease enter row number (1-12): ";
  37. cin >> a;
  38. } while (a <1 || a > 12);
  39. do
  40. {
  41. cout << "\nPlease enter seat number (2-3): ";
  42. cin >> b;
  43. if (string(b, 'A') ==0)
  44. {
  45. seatmap[z][b] == '*';
  46. }
  47. else
  48. {
  49. Sorry();
  50. } while ( b < 1 || b > 4);
  51. }
  52. }
  53. if (type == "window")
  54. {
  55. do
  56. {
  57. for (int z = 0; z < 4; z++)
  58. {
  59. for (int x = 0; x < 1; x++)
  60. for (int w = 3; w <4; w++)
  61. {
  62. cout << "\nPlease enter row number (1-12): ";
  63. cin >> a;
  64. } while (a <1 || a > 12);
  65. do
  66. {
  67. cout << "\nPlease enter seat number (1 or 4): ";
  68. cin >> b;
  69. if (string(b, 'W') ==0)
  70. {
  71. seatmap[z][b] == '*';
  72. }
  73. else
  74. {
  75. Sorry();
  76. }
  77. } while ( b < 1 || b > 4);
  78. }
  79. }
  80. }

This is my best (miserable) attempt.
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 27th, 2005
0

Re: airplane seating prices

now compare yours with mine. Ive changed a couple of things, displaying now displays row and seatnumbers. Sorry has gone. There are 2 overloads of AllocSeat. one lets you pick a specific seat and another a type of seat. Have you covered overloading of functions yet?
Starting to see how this is coming together?
C++ Syntax (Toggle Plain Text)
  1. // You have shown enough effort for me to help you rewrite this
  2. // We will use standard headers and not the deprecated ones you were using.
  3. // I am assuming you are running under a windows OS so we can use a few
  4. // facilities provided by the OS to do things like clearing the screen.
  5.  
  6. #include<iostream>
  7. #include<iomanip>
  8. #include<string>
  9. #include<limits>
  10. #include<windows.h>
  11. // windows.h defines a max macro that will make our life hell so we undef it
  12. #undef max
  13.  
  14. // the functions and objects in the standard headers are all declared within namespace
  15. // std. We will bring them all into the global scope so we dont have to prefix everything
  16. // with std:: with this simple line.
  17. using namespace std;
  18.  
  19. // here is how to clear the screen on a windows console. You do not need to
  20. // necessarily understand this to use it.Suffice to say that a call to this
  21. // will clear the screen and reset the cursor pos to the top left corner.
  22. void clrscr()
  23. {
  24. COORD coordScreen = { 0, 0 };
  25. DWORD cCharsWritten;
  26. CONSOLE_SCREEN_BUFFER_INFO csbi;
  27. DWORD dwConSize;
  28. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  29. GetConsoleScreenBufferInfo(hConsole, &csbi);
  30. dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  31. FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
  32. GetConsoleScreenBufferInfo(hConsole, &csbi);
  33. FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
  34. SetConsoleCursorPosition(hConsole, coordScreen);
  35. }
  36.  
  37. // Lets introduce a struct to represent our seats. A struct is an aggregate type made up
  38. // of smaller types.
  39. // You declare one like so :-
  40. // struct STRUCTNAME { members };
  41. // you make an object of your struct like declaring an int.
  42. // STRUCTNAME obj;
  43. // You access the members with the dot operator like so...
  44. // struct mystruct{inta;intb;};
  45. // mystruct astruct;
  46. // astruct.a = 0;astruct.b = 0;
  47. struct Seat
  48. {
  49. string seattype;
  50. float seatprice;
  51. int row;
  52. int seatnumber;
  53. bool taken; // a bool has 2 states true and false. When this is true the seat is taken
  54. };
  55.  
  56. // Now we will make a global array of seats to represent the plane.
  57. // We will use this all over the place so global as a convenience.
  58. // There are 12 rows of 4 seats so...
  59. Seat seatmap[12][4];
  60.  
  61. // Lets write a function to get the prices of seats
  62. float GetInput(string s)
  63. {
  64. cout<<s;
  65. float input;
  66. while (!(cin>>input))
  67. {
  68. cerr<<endl<<"error! re-enter"<<endl;
  69. cin.clear();
  70. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  71. }
  72. return input;
  73. }
  74.  
  75. // armed with that we can write a function to initalise the seatmap
  76. void InitSeatMap()
  77. {
  78. float Legroomprice = GetInput(string("Enter cost of a leg-room seat ? "));
  79. float Windowprice = GetInput(string("Enter cost of a window seat ? "));
  80. float Aisleprice = GetInput(string("Enter cost of an aisle seat ? "));
  81. for (int i=0;i<12;++i)
  82. for(int j=0;j<4;++j)
  83. {
  84. seatmap[i][j].row = i+1;
  85. seatmap[i][j].seatnumber = j+1;
  86. seatmap[i][j].taken = false; // no seat is taken yet
  87. if (i==0) // legroom seats
  88. {
  89. seatmap[i][j].seattype = "legroom";
  90. seatmap[i][j].seatprice = Legroomprice;
  91. }
  92. if((i != 0) && (j==0 || j==3)) // we have a windowseat
  93. {
  94. seatmap[i][j].seattype = "window";
  95. seatmap[i][j].seatprice = Windowprice;
  96. }
  97. if((i != 0) && (j==1 || j==2)) // we have an aisle seat
  98. {
  99. seatmap[i][j].seattype = "aisle";
  100. seatmap[i][j].seatprice = Aisleprice;
  101. }
  102. }
  103. }
  104.  
  105. // now we can write a function to allocate a seat
  106. void AllocSeat(string type)
  107. {
  108. for(int i=0;i<12;++i)
  109. for(int j=0;j<4;++j)
  110. {
  111. if (seatmap[i][j].seattype == type && !seatmap[i][j].taken)
  112. {
  113. seatmap[i][j].taken = true; // seats free so allocate it taken
  114. return;
  115. }
  116. }
  117. cout<<endl<<"Sorry but that type of seat is not available!!!"<<endl;
  118. }
  119.  
  120. // an overload to take a specific seat
  121. void AllocSeat(int row, int seatnum)
  122. {
  123. if (!seatmap[row-1][seatnum-1].taken)
  124. {
  125. seatmap[row-1][seatnum-1].taken = true;
  126. return;
  127. }
  128. cout<<endl<<"Sorry but that particular seat is already taken!!!"<<endl;
  129. }
  130.  
  131. void Menu()
  132. {
  133. clrscr();
  134. // exercise for reader
  135. }
  136.  
  137. void DisplaySeatMap()
  138. {
  139. clrscr();
  140. for (int i=0;i<12;++i)
  141. for (int j=0;j<4;++j)
  142. {
  143. if (!i && !j)
  144. {
  145. cout<<" "<<setw(3)<<1<<setw(3)<<2<<setw(3)<<3<<setw(3)<<4<<endl;
  146. }
  147. if (j==0)
  148. {
  149. cout<<setw(4)<<left<<seatmap[i][j].row;
  150. }
  151. if (seatmap[i][j].seattype == "legroom")
  152. {
  153. cout <<setw(3)<<left<<((seatmap[i][j].taken) ? "*" : "L");
  154. }
  155. if (seatmap[i][j].seattype == "window")
  156. {
  157. cout <<setw(3)<<left<<((seatmap[i][j].taken) ? "*" : "W");
  158. }
  159. if (seatmap[i][j].seattype == "aisle")
  160. {
  161. cout <<setw(3)<<left<<((seatmap[i][j].taken) ? "*" : "A");
  162. }
  163. if (j==3)
  164. cout<<endl;
  165. }
  166. }
  167.  
  168. int main()
  169. {
  170. InitSeatMap();
  171.  
  172. // exercise for reader
  173.  
  174. return 0;
  175. }
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Jul 27th, 2005
0

Re: airplane seating prices

Ok, I've compared yours with mine and they're not EVEN close to being similar to each other.

I don't understand why you have 2 AlloSeat() functions, are you allowed to do that? How do we know which function to use in the main function?

And no, I've learned about overloading functions yet.
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 27th, 2005
0

Re: airplane seating prices

OMG! My final (for another class) is in 30 min! I have to go to that class! When will you be gone?
This airplane project is due tomorrow, cause tomorrow is my final in this C++ class!
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 27th, 2005
0

Re: airplane seating prices

yes you are allowed to do that its called function overloading. If we called it like this...

string s="legroom";
AllocSeat(s);

then the one that takes a string will be called. This will automatically search for the first available seat of that type and allocate it. I should have added a message telling you seatnumber and row, you can do that i am sure.
Alternatively if we call it like so

int row,seatnum;
cout<<"enter row and seatnumber separated by a space"<<endl;
cin>>row>>seatnum;
AllocSeat(row,seatnum);

then that overload will try to take the specific seat pointed at by row/seatnum.

So you should now be able to write a function that asks user whether he would like to pick a seat or auto get one and call the correct overload of AllocSeat.
Again dont panic, I know this is due soon but we will get it done and my hope is I teach you a little something on the way too.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Jul 27th, 2005
0

Re: airplane seating prices

maybe 2.5 hrs more. then 8 hrs sleep then on for a bit before work over brekkies.
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