MD 阵列未更新

MD array not updating

本文关键字:更新 阵列 MD      更新时间:2023-10-16

我正在尝试完成编程入门的家庭作业项目。我们刚刚讨论了多维数组。

我们应该设计一个程序,该程序必不可少地充当飞机上预订座位的系统。您选择一个座位,如果可用,则用"X"字符标记,表示它已被选中。如果您选择的座位不可用(标有"X"),则系统会告诉您该座位不可用,并可以选择再次选择。每次选择后,飞机的座位图都会更新。

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

  1. 阿 乙 中 德
  2. 阿 乙 中 德
  3. 阿 乙 中 德
  4. 阿 乙 中 德
  5. 阿 乙 中 德
  6. 阿 乙 中 德
  7. 阿 乙 中 德

    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;
    

    }

你需要处理数组索引。

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';
}