2017-04-12 55 views
0

我正在努力完成一個家庭作業項目,爲我的編程課程介紹。我們剛剛討論了多維數組。MD數組沒有更新

我們應該設計一個程序,這個程序在飛機上預訂座位時必不可少。你選擇一個座位,如果有可用的話,它會標上一個'X'字符來表示它已被選中。如果您選擇一個不可用的座位(標有'X'),那麼您會被告知座位不可用,並且可以選擇再試一次。每次選擇後,飛機的座位圖都會更新。

我一直在遇到的問題是地圖沒有正確更新。在詢問我的教授一些幫助後,她建議我改變循環,但是,我不確定我會如何改變它。有人可以給我一些幫助嗎? (以下信息): 這裏的座位圖的樣子:

  1. ABCD
  2. ABCD
  3. ABCD
  4. ABCD
  5. ABCD
  6. ABCD
  7. ABCD

    int row, col; 
    char seats[7][5], choice; 
    
    ifstream input; 
    input.open("plane.txt"); 
    
    for(int a = 0; a<7; a++) 
    {  cout<<"\n"; 
         for(int b = 0; b<5; b++) 
         {  input>>seats[a][b]; 
           cout<<seats[a][b]<<" "; 
         } 
    } 
    
    
    do 
    {  cout<<"What row would you like to sit in?\n"; 
         cin>>row; 
         cout<<"What seat would you like? (A=1, B=2, C=3, D=5)"; 
         cin>>col; 
         if(seats[row][col]=='X') 
           cout<<"Sorry this seat is already taken. Try another.\n"; 
         else 
         {  cout<<"Your seat has been reserved."; 
           cout<<" Be sure to check the updated seat chart to confirm your order."; 
           seats[row][col]='X'; 
         } 
         for(int c = 0; c<7; c++) 
         {  cout<<"\n"; 
           for(int d = 0; d<5; d++) 
           {  cout<<seats[c][d]<<" "; 
           } 
         } 
         cout<<"\n"; 
         cout<<" Would you like to pick another seat? (Y/N)"; 
         cin>>choice; 
    } 
    while((choice=='y')||(choice=='Y')); 
    return 0; 
    

    }

+2

使用調試器進行調試,您會發現正在發生的事情。 –

+5

當你詢問'row'和'col'輸入並使用這些變量時,你似乎忘記了數組索引是從零開始的。 –

+2

這個例子工作正常,只要你注意不要提供出界索引。 –

回答

0

你需要採取數組索引的照顧。

if(seats[row-1][col-1]=='X') 
    cout<<"Sorry this seat is already taken. Try another.\n"; 
else {  
    cout<<"Your seat has been reserved."; 
    cout<<" Be sure to check the updated seat chart to confirm your order."; 
    seats[row-1][col-1]='X'; 
}