943,708 Members | Top Members by Rank

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

Re: airplane seating prices

'K I'll try to have everything done (hopefully not too many misktakes) tonight. When can you check it tomorrow?

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

here I cleaned it up a bit and added some while smoking a joint. Theres not much left for you to do now. Make sure you understand everything. If you are not sure then ask.
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<cstdlib>
  11. #include<windows.h>
  12. // windows.h defines a max macro that will make our life hell so we undef it
  13. #undef max
  14.  
  15. // the functions and objects in the standard headers are all declared within namespace
  16. // std. We will bring them all into the global scope so we dont have to prefix everything
  17. // with std:: with this simple line.
  18. using namespace std;
  19.  
  20. // here is how to clear the screen on a windows console. You do not need to
  21. // necessarily understand this to use it.Suffice to say that a call to this
  22. // will clear the screen and reset the cursor pos to the top left corner.
  23. void clrscr()
  24. {
  25. COORD coordScreen = { 0, 0 };
  26. DWORD cCharsWritten;
  27. CONSOLE_SCREEN_BUFFER_INFO csbi;
  28. DWORD dwConSize;
  29. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  30. GetConsoleScreenBufferInfo(hConsole, &csbi);
  31. dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  32. FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
  33. GetConsoleScreenBufferInfo(hConsole, &csbi);
  34. FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
  35. SetConsoleCursorPosition(hConsole, coordScreen);
  36. }
  37.  
  38. // Lets introduce a struct to represent our seats. A struct is an aggregate type made up
  39. // of smaller types.
  40. // You declare one like so :-
  41. // struct STRUCTNAME { members };
  42. // you make an object of your struct like declaring an int.
  43. // STRUCTNAME obj;
  44. // You access the members with the dot operator like so...
  45. // struct mystruct{inta;intb;};
  46. // mystruct astruct;
  47. // astruct.a = 0;astruct.b = 0;
  48. struct Seat
  49. {
  50. string seattype;
  51. float seatprice;
  52. int row;
  53. int seatnumber;
  54. bool taken; // a bool has 2 states true and false. When this is true the seat is taken
  55. };
  56.  
  57. // Now we will make a global array of seats to represent the plane.
  58. // We will use this all over the place so global as a convenience.
  59. // There are 12 rows of 4 seats so...
  60. Seat seatmap[12][4];
  61.  
  62. // Lets write a function to get the prices of seats
  63. float GetInput(string s)
  64. {
  65. cout<<s;
  66. float input;
  67. while (!(cin>>input))
  68. {
  69. cerr<<endl<<"error! re-enter"<<endl;
  70. cin.clear();
  71. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  72. }
  73. return input;
  74. }
  75.  
  76. // armed with that we can write a function to initalise the seatmap
  77. void InitSeatMap()
  78. {
  79. float Legroomprice = GetInput(string("Enter cost of a leg-room seat ? "));
  80. float Windowprice = GetInput(string("Enter cost of a window seat ? "));
  81. float Aisleprice = GetInput(string("Enter cost of an aisle seat ? "));
  82. for (int i=0;i<12;++i)
  83. for(int j=0;j<4;++j)
  84. {
  85. seatmap[i][j].row = i+1;
  86. seatmap[i][j].seatnumber = j+1;
  87. seatmap[i][j].taken = false; // no seat is taken yet
  88. if (i==0) // legroom seats
  89. {
  90. seatmap[i][j].seattype = "legroom";
  91. seatmap[i][j].seatprice = Legroomprice;
  92. }
  93. if((i != 0) && (j==0 || j==3)) // we have a windowseat
  94. {
  95. seatmap[i][j].seattype = "window";
  96. seatmap[i][j].seatprice = Windowprice;
  97. }
  98. if((i != 0) && (j==1 || j==2)) // we have an aisle seat
  99. {
  100. seatmap[i][j].seattype = "aisle";
  101. seatmap[i][j].seatprice = Aisleprice;
  102. }
  103. }
  104. }
  105.  
  106. // now we can write a function to allocate a seat
  107. void AllocSeat(string type)
  108. {
  109. for(int i=0;i<12;++i)
  110. for(int j=0;j<4;++j)
  111. {
  112. if (seatmap[i][j].seattype == type && !seatmap[i][j].taken)
  113. {
  114. seatmap[i][j].taken = true; // seats free so allocate it taken
  115. cout<<endl<<"Seat allocated. Row :- "<<seatmap[i][j].row<< "Seatnumber :- "<<seatmap[i][j].seatnumber<<endl;
  116. return;
  117. }
  118. }
  119. cout<<endl<<"Sorry but that type of seat is not available!!!"<<endl;
  120. }
  121.  
  122. // an overload to take a specific seat
  123. void AllocSeat(int row, int seatnum)
  124. {
  125. if (!seatmap[row-1][seatnum-1].taken)
  126. {
  127. seatmap[row-1][seatnum-1].taken = true;
  128. cout<<endl<<"Seat allocated."<<endl;
  129. return;
  130. }
  131. cout<<endl<<"Sorry but that particular seat is already taken!!!"<<endl;
  132. }
  133.  
  134. void Menu()
  135. {
  136. clrscr();
  137. cout<<"1 - View available seats"<<endl
  138. <<"2 - View seating prices"<<endl
  139. <<"3 - View ticket sales"<<endl
  140. <<"4 - Purchase a ticket"<<endl
  141. <<"5 - Quit"<<endl;
  142. }
  143.  
  144. void DisplaySeatMap()
  145. {
  146. clrscr();
  147. for (int i=0;i<12;++i)
  148. for (int j=0;j<4;++j)
  149. {
  150. if (!i && !j)
  151. {
  152. cout<<" "<<setw(3)<<1<<setw(3)<<2<<setw(3)<<3<<setw(3)<<4<<endl;
  153. }
  154. if (j==0)
  155. {
  156. cout<<setw(4)<<left<<seatmap[i][j].row;
  157. }
  158. if (seatmap[i][j].seattype == "legroom")
  159. {
  160. cout <<setw(3)<<left<<((seatmap[i][j].taken) ? "*" : "L");
  161. }
  162. if (seatmap[i][j].seattype == "window")
  163. {
  164. cout <<setw(3)<<left<<((seatmap[i][j].taken) ? "*" : "W");
  165. }
  166. if (seatmap[i][j].seattype == "aisle")
  167. {
  168. cout <<setw(3)<<left<<((seatmap[i][j].taken) ? "*" : "A");
  169. }
  170. if (j==3)
  171. cout<<endl;
  172. }
  173. system("PAUSE"); // lousy but just what we need
  174. }
  175.  
  176. void PurchaseSeat()
  177. {
  178. // offer pick a seat or type of seat.
  179. // call correct AllocSeat
  180. // display cost
  181. }
  182.  
  183. void DisplaySales()
  184. {
  185. int sales[3]={0};
  186. for (int i=0;i<12;++i)
  187. for (int j=0;j<4;++j)
  188. {
  189. if (seatmap[i][j].seattype == "legroom" && seatmap[i][j].taken)
  190. {
  191. ++sales[0];
  192. }
  193. if (seatmap[i][j].seattype == "window" && seatmap[i][j].taken)
  194. {
  195. ++sales[1];
  196. }
  197. if (seatmap[i][j].seattype == "aisle" && seatmap[i][j].taken)
  198. {
  199. ++sales[2];
  200. }
  201. }
  202. clrscr();
  203. cout<<"Legroom seats sold :- "<<sales[0]<<" at $"<<seatmap[0][0].seatprice<<endl
  204. <<"Window seats sold :- "<<sales[1]<<" at $"<<seatmap[1][0].seatprice<<endl
  205. <<"Aisle seats sold :- "<<sales[2]<<" at $"<<seatmap[1][1].seatprice<<endl;
  206. double revenue = (sales[0] * seatmap[0][0].seatprice) + (sales[1] * seatmap[1][0].seatprice)
  207. + (sales[2] * seatmap[1][1].seatprice);
  208. cout<<"Total revenues for this flight are $"<<revenue<<endl;
  209. system("PAUSE");
  210. }
  211.  
  212. void DisplayPricing()
  213. {
  214. clrscr();
  215. cout << "Legroom seat :- $"<<seatmap[0][0].seatprice<<endl
  216. <<"Window seat :- $"<<seatmap[1][0].seatprice<<endl
  217. <<"Aisle seat :- $"<<seatmap[1][1].seatprice<<endl;
  218. system("PAUSE"); // lousy but does the job
  219. }
  220.  
  221.  
  222. int main()
  223. {
  224. InitSeatMap();
  225. while(1) // infinite loop
  226. {
  227. Menu();
  228. cout<<"Enter choice :- ";
  229. int input;
  230. while (!(cin>>input) || input<0 || input>5)
  231. {
  232. cerr<<endl<<"error! re-enter"<<endl;
  233. cin.clear();
  234. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  235. }
  236. switch(input)
  237. {
  238. case 1: DisplaySeatMap();
  239. break;
  240. case 2: DisplayPricing();
  241. break;
  242. case 3: DisplaySales();
  243. break;
  244. case 4: PurchaseSeat();
  245. break;
  246. case 5: cout<<"Exiting.........Goodbye!!!"<<endl;
  247. Sleep(2000); // a small wait of 2 secs
  248. return 0;
  249. }
  250. }
  251. }
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Jul 28th, 2005
0

Re: airplane seating prices

gotta go work now so heres how mine looks finished.
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<cstdlib>
  11. #include<windows.h>
  12. // windows.h defines a max macro that will make our life hell so we undef it
  13. #undef max
  14.  
  15. // the functions and objects in the standard headers are all declared within namespace
  16. // std. We will bring them all into the global scope so we dont have to prefix everything
  17. // with std:: with this simple line.
  18. using namespace std;
  19.  
  20. // here is how to clear the screen on a windows console. You do not need to
  21. // necessarily understand this to use it.Suffice to say that a call to this
  22. // will clear the screen and reset the cursor pos to the top left corner.
  23. void clrscr()
  24. {
  25. COORD coordScreen = { 0, 0 };
  26. DWORD cCharsWritten;
  27. CONSOLE_SCREEN_BUFFER_INFO csbi;
  28. DWORD dwConSize;
  29. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  30. GetConsoleScreenBufferInfo(hConsole, &csbi);
  31. dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  32. FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
  33. GetConsoleScreenBufferInfo(hConsole, &csbi);
  34. FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
  35. SetConsoleCursorPosition(hConsole, coordScreen);
  36. }
  37.  
  38. // Lets introduce a struct to represent our seats. A struct is an aggregate type made up
  39. // of smaller types.
  40. // You declare one like so :-
  41. // struct STRUCTNAME { members };
  42. // you make an object of your struct like declaring an int.
  43. // STRUCTNAME obj;
  44. // You access the members with the dot operator like so...
  45. // struct mystruct{inta;intb;};
  46. // mystruct astruct;
  47. // astruct.a = 0;astruct.b = 0;
  48. struct Seat
  49. {
  50. string seattype;
  51. float seatprice;
  52. int row;
  53. int seatnumber;
  54. bool taken; // a bool has 2 states true and false. When this is true the seat is taken
  55. };
  56.  
  57. // Now we will make a global array of seats to represent the plane.
  58. // We will use this all over the place so global as a convenience.
  59. // There are 12 rows of 4 seats so...
  60. Seat seatmap[12][4];
  61.  
  62. // Lets write a function to get the prices of seats
  63. float GetInput(string s)
  64. {
  65. cout<<s;
  66. float input;
  67. while (!(cin>>input))
  68. {
  69. cerr<<endl<<"error! re-enter"<<endl;
  70. cin.clear();
  71. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  72. }
  73. return input;
  74. }
  75.  
  76. // armed with that we can write a function to initalise the seatmap
  77. void InitSeatMap()
  78. {
  79. float Legroomprice = GetInput(string("Enter cost of a leg-room seat ? "));
  80. float Windowprice = GetInput(string("Enter cost of a window seat ? "));
  81. float Aisleprice = GetInput(string("Enter cost of an aisle seat ? "));
  82. for (int i=0;i<12;++i)
  83. for(int j=0;j<4;++j)
  84. {
  85. seatmap[i][j].row = i+1;
  86. seatmap[i][j].seatnumber = j+1;
  87. seatmap[i][j].taken = false; // no seat is taken yet
  88. if (i==0) // legroom seats
  89. {
  90. seatmap[i][j].seattype = "legroom";
  91. seatmap[i][j].seatprice = Legroomprice;
  92. }
  93. if((i != 0) && (j==0 || j==3)) // we have a windowseat
  94. {
  95. seatmap[i][j].seattype = "window";
  96. seatmap[i][j].seatprice = Windowprice;
  97. }
  98. if((i != 0) && (j==1 || j==2)) // we have an aisle seat
  99. {
  100. seatmap[i][j].seattype = "aisle";
  101. seatmap[i][j].seatprice = Aisleprice;
  102. }
  103. }
  104. }
  105.  
  106. // now we can write a function to allocate a seat
  107. void AllocSeat(string type)
  108. {
  109. for(int i=0;i<12;++i)
  110. for(int j=0;j<4;++j)
  111. {
  112. if (seatmap[i][j].seattype == type && !seatmap[i][j].taken)
  113. {
  114. seatmap[i][j].taken = true; // seats free so allocate it taken
  115. cout<<endl<<"Seat allocated. Row :- "<<seatmap[i][j].row<< " Seatnumber :- "<<seatmap[i][j].seatnumber<<endl;
  116. cout<<"Cost of seat is $"<<seatmap[i][j].seatprice<<endl;
  117. return;
  118. }
  119. }
  120. cout<<endl<<"Sorry but that type of seat is not available!!!"<<endl;
  121. }
  122.  
  123. // an overload to take a specific seat
  124. void AllocSeat(int row, int seatnum)
  125. {
  126. if (!seatmap[row-1][seatnum-1].taken)
  127. {
  128. seatmap[row-1][seatnum-1].taken = true;
  129. cout<<endl<<"Seat allocated."<<endl;
  130. cout<<"Cost of seat is $"<<seatmap[row-1][seatnum-1].seatprice<<endl;
  131. return;
  132. }
  133. cout<<endl<<"Sorry but that particular seat is already taken!!!"<<endl;
  134. }
  135.  
  136. void Menu()
  137. {
  138. clrscr();
  139. cout<<"1 - View available seats"<<endl
  140. <<"2 - View seating prices"<<endl
  141. <<"3 - View ticket sales"<<endl
  142. <<"4 - Purchase a ticket"<<endl
  143. <<"5 - Quit"<<endl;
  144. }
  145.  
  146. void DisplaySeatMap()
  147. {
  148. clrscr();
  149. for (int i=0;i<12;++i)
  150. for (int j=0;j<4;++j)
  151. {
  152. if (!i && !j)
  153. {
  154. cout<<" "<<setw(3)<<1<<setw(3)<<2<<setw(3)<<3<<setw(3)<<4<<endl;
  155. }
  156. if (j==0)
  157. {
  158. cout<<setw(4)<<left<<seatmap[i][j].row;
  159. }
  160. if (seatmap[i][j].seattype == "legroom")
  161. {
  162. cout <<setw(3)<<left<<((seatmap[i][j].taken) ? "*" : "L");
  163. }
  164. if (seatmap[i][j].seattype == "window")
  165. {
  166. cout <<setw(3)<<left<<((seatmap[i][j].taken) ? "*" : "W");
  167. }
  168. if (seatmap[i][j].seattype == "aisle")
  169. {
  170. cout <<setw(3)<<left<<((seatmap[i][j].taken) ? "*" : "A");
  171. }
  172. if (j==3)
  173. cout<<endl;
  174. }
  175. system("PAUSE"); // lousy but just what we need
  176. }
  177.  
  178. bool Valid(char c)
  179. {
  180. c = tolower(c);
  181. if( c == 'a' || c == 'w' || c == 'l')
  182. return true;
  183. else
  184. return false;
  185. }
  186.  
  187. void PurchaseSeat()
  188. {
  189. clrscr();
  190. cout<<"Do you wish to pick a particular seat(Y/N) ? ";
  191. char input;
  192. while(!(cin>>input))
  193. {
  194. cerr<<endl<<"error! re-enter"<<endl;
  195. cin.clear();
  196. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  197. }
  198. if (tolower(input) == 'y')
  199. {
  200. int row,seatnum;
  201. cout<<"Enter row followed by a space then seat number :- ";
  202. while (!(cin>>row>>seatnum) || row<1 || row>12 || seatnum<1 || seatnum>4)
  203. {
  204. cerr<<endl<<"error! re-enter"<<endl;
  205. cin.clear();
  206. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  207. }
  208. AllocSeat(row,seatnum);
  209. }
  210. else
  211. {
  212. cout<<endl<<"What type of seat would you like (W,L,A) ? ";
  213. while(!(cin>>input) || !Valid(input))
  214. {
  215. cerr<<endl<<"error! re-enter"<<endl;
  216. cin.clear();
  217. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  218. }
  219. string type;
  220. if(tolower(input) == 'l')
  221. {
  222. type = "legroom";
  223. }
  224. else if(tolower(input) == 'w')
  225. {
  226. type = "window";
  227. }
  228. else
  229. {
  230. type = "aisle";
  231. }
  232. AllocSeat(type);
  233. system("PAUSE");
  234. }
  235. }
  236.  
  237. void DisplaySales()
  238. {
  239. int sales[3]={0};
  240. for (int i=0;i<12;++i)
  241. for (int j=0;j<4;++j)
  242. {
  243. if (seatmap[i][j].seattype == "legroom" && seatmap[i][j].taken)
  244. {
  245. ++sales[0];
  246. }
  247. if (seatmap[i][j].seattype == "window" && seatmap[i][j].taken)
  248. {
  249. ++sales[1];
  250. }
  251. if (seatmap[i][j].seattype == "aisle" && seatmap[i][j].taken)
  252. {
  253. ++sales[2];
  254. }
  255. }
  256. clrscr();
  257. cout<<"Legroom seats sold :- "<<sales[0]<<" at $"<<seatmap[0][0].seatprice<<endl
  258. <<"Window seats sold :- "<<sales[1]<<" at $"<<seatmap[1][0].seatprice<<endl
  259. <<"Aisle seats sold :- "<<sales[2]<<" at $"<<seatmap[1][1].seatprice<<endl;
  260. double revenue = (sales[0] * seatmap[0][0].seatprice) + (sales[1] * seatmap[1][0].seatprice)
  261. + (sales[2] * seatmap[1][1].seatprice);
  262. cout<<"Total revenues for this flight are $"<<revenue<<endl;
  263. system("PAUSE");
  264. }
  265.  
  266. void DisplayPricing()
  267. {
  268. clrscr();
  269. cout << "Legroom seat :- $"<<seatmap[0][0].seatprice<<endl
  270. <<"Window seat :- $"<<seatmap[1][0].seatprice<<endl
  271. <<"Aisle seat :- $"<<seatmap[1][1].seatprice<<endl;
  272. system("PAUSE"); // lousy but does the job
  273. }
  274.  
  275.  
  276. int main()
  277. {
  278. InitSeatMap();
  279. while(1) // infinite loop
  280. {
  281. Menu();
  282. cout<<"Enter choice :- ";
  283. int input;
  284. while (!(cin>>input) || input<1 || input>5)
  285. {
  286. cerr<<endl<<"error! re-enter"<<endl;
  287. cin.clear();
  288. cin.ignore(numeric_limits<streamsize>::max(),'\n');
  289. }
  290. switch(input)
  291. {
  292. case 1: DisplaySeatMap();
  293. break;
  294. case 2: DisplayPricing();
  295. break;
  296. case 3: DisplaySales();
  297. break;
  298. case 4: PurchaseSeat();
  299. break;
  300. case 5: cout<<"Exiting.........Goodbye!!!"<<endl;
  301. Sleep(2000); // a small wait of 2 secs
  302. return 0;
  303. }
  304. }
  305. }
Did you manage to make those changes on your own?
Anything you didn't understand?
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Jul 28th, 2005
0

Re: airplane seating prices

Hi, Stone_Coder;
thank you so much for helping me out.
I don't know if this is hard work for you or not (prob not), but it was certainly was for me. And I'm glad that you help me through it.
Now I'm not going to lie and say I understood your code. I said I can follow it, not nessarily understood everything. Now that the class is over and it's not rushed, I'm going to go through your code slowly. And it might take me a bit of time to digest everything. So if I'm not posting my questions right away yet, please don't think I'm abandoning it because the project's over and done with (plus I don't think you'd appreciate it if I post everything question I have, 'cause it's quite a lot).
When you said "hopefully" you'd teach me something along the way, you don't know how right you are! I certainly learn a lot from doing this, both from my own experience and from your help.

Oh, I forgot. My PurchaseTicket() function, again, did not look like yours at all. I keep having problems with the structure data thing. And I couldn't get it linked to the DisplaySales() functions. But I understand what I did wrong now...I think.

So I know I said it alot already, but Thanks again!

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

Re: airplane seating prices

And for those who weren't aware of the cross-posting, there's this:
http://www.gidforums.com/t-6485.html

Where else this may be I don't know. But it is always nice for the OP to tell others that the question may already be answered elsewhere.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 29th, 2005
0

Re: airplane seating prices

yeah, I was about to do that tonight. I just got it done today.
but thanks for the reminder though.
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 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