MD阵列没有更新

时间:2017-04-12 14:40:52

标签: c++ arrays loops multidimensional-array filestream

我正在尝试为编程课程的介绍完成一个家庭作业项目。我们刚刚讨论了多维数组。

我们应该设计一个必不可少的程序,作为预订飞机座位的系统。您选择一个座位,如果可用,则标有“X”字符表示已被选中。如果你选择一个不可用的座位(标有“X”),那么你会被告知座位不可用,并且可以选择再次尝试。每次选择后,飞机座位图都会更新。

我遇到的问题是地图没有正确更新。在向我的教授寻求帮助之后,她建议我改变循环但是,我不确定如何改变它。有人可以给我一些帮助吗? (以下信息): 这是座位图的样子:

  1. A B C D
  2. A B C D
  3. A B C D
  4. A B C D
  5. A B C D
  6. A B C D
  7. A B C D

    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;
    

    }

1 个答案:

答案 0 :(得分: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';
}