2017-04-12 57 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'; 
}