2013-11-26 34 views
-6

所以我正在研究劇院座位問題。程序的輸出結果並不符合它的要求。我需要幫助排列#和*與他們適當的專欄。還按Q退出不起作用。此外,我需要弄清楚如何從文件中讀取座位表信息。如果帶有座位信息的文件尚不存在,則表示所有座位都爲空,即 。每當程序結束時,座位圖信息應存儲在此文件中。任何其他提示也會有所幫助。這裏是我的代碼到目前爲止:複雜的C++作業

#include "stdafx.h" 
# include <iostream> 
# include <iomanip> 
using namespace std; 

void seats(double [] , int); 
void mapSeats(); 
char movieMenu(char); 

int main() 
{ 
    const int rowNum = (15.0); 
    double rowValue[rowNum]; //array to hold row pices 
    char selection; 
    int row2, col2; 
    const char TAKEN = '#';//seats taken 
    const char EMPTY = '*';//seats free 
    const int row = 15;//number of rows 
    const int col = 20;//number of col 
    char map[row][col];//array to hold seat chart 



for(int i= 0;i<row;i++)//initiating array 
{ 
    for (int j=0;j<col;j++) 
    { 
     map[i][j]=EMPTY; 
    } 
} 

mapSeats(); 

seats(rowValue, rowNum);//ask user to enter price of each row 
cout << endl; 

do 
    { 
     cout << "MOVIE THEATER MENU" << endl; 
     cout << "------------------" << endl; 
     cout << "1) Sell a ticket" << endl; 
     cout << "Q) Quit program" << endl; 
     cout << "Please make a selection: "; 
     cin >> selection; 

     if(selection =='1') 
     { 
      cout << "Please enter a row number and a seat number for the ticket: " ; 
      cout << "Row # :" ; 
      cin >> row2; 
      cout << endl; 
      cout << "Seat # :" ; 
      cin >> col2; 
      cout << endl; 

      // Check if seat is free 
     if(map[row2][col2] == TAKEN) { 

      cout << "This seat is taken! Try another one. \n"; 
      continue; // start the loop again 
        } 
      else // and if it is - sell the ticket 
      map[row2][col2]=TAKEN; 
     // Add the next loop to immediately see the effects: 
      for (int i = 0; i < row; i++){ 
       for(int j = 0; j < col; j++){ 
       cout << map[i][j]; 
       } 
cout << endl; 
    } 

     } 
     else if(selection =='q'||selection=='Q') 
     { 
      cout << "Thank you for using the program." << endl; 
     } 
     else if(selection != '1' || selection !='q' || selection !='Q') 
     { 
      cout << "Invalid selection." << endl; 
     } 
    }while(selection != '1' || selection !='q' || selection !='Q'); 


system("pause"); 
return 0; 
    } 


    void seats(double rowPrice[], int row) 
    { 

cout << "Please enter a ticket price for each row." << endl; 

for(int i = 0 ; i < row; i++) 
{ 
    cout << "Row # " << i+1 << ": " ; 
    cin >> rowPrice[i]; 
} 
    } 

    void mapSeats() 
    { 
    const char TAKEN = '#';//seats taken 
    const char EMPTY = '*';//seats free 
    const int rw=20; 
    const int cl=15; 

    cout << "Seats " ; 
    for(int k = 0 ; k <20;k++) //loop to display nums 0 to 19 
    { 
    cout << fixed<< setw(2) << " " << k ; 
    } 

for(int i=0;i<rw;i++)//making array display what's in it 
{ 
    cout << endl<< "Row " << i; 
    for(int j=0;j<cl;j++) 
    { 
     cout << fixed<< setw(2) << "" << EMPTY; 
    } 
} 
cout << endl; 
    } 
+3

堆棧溢出不是調試器或代碼生成器。 –

+2

歡迎來到[so]。當問一個問題時,提供關於什麼不起作用的具體問題以及它不起作用是很有用的。問題應該詢問有關解決問題的嘗試,以及詳細描述問題。有關更多信息,請參見[問]。謝謝! –

+1

無論如何,您可能想要了解[調試您的代碼]的基礎知識(http://www.learncpp.com/cpp-tutorial/a4-debugging-your-program-stepping-and-breakpoints)。 –

回答

2

Q退出不起作用。你的邏輯是錯誤的

do 
{ 
    ... 
} while (selection !='q' && selection !='Q'); 

你堅持下去,而選擇不是「Q」 而選擇不是「Q」。新手得到'或'和'和'混合起來很常見。

+0

+1更長,但是「您的邏輯錯誤」,那麼某些代碼聽起來像是在蒸餾破損的代碼,而不是呈現固定代碼... :-) –