C++ 如何在c++;
我必须制作一个程序,读取座位数并将其存储在二维数组中。空座位是标签,如果用户购买了座位,它就会变成*。奇数排有15个座位,偶数排有20个。 当我买一个座位时,它会将*放在座位上,但当我买另一个座位时,它会将*移走,并将*放在新买的座位上。我怎样才能使它保存打印在每个座位上的*信息 全球的C++ 如何在c++;,c++,arrays,for-loop,arraylist,multidimensional-array,C++,Arrays,For Loop,Arraylist,Multidimensional Array,我必须制作一个程序,读取座位数并将其存储在二维数组中。空座位是标签,如果用户购买了座位,它就会变成*。奇数排有15个座位,偶数排有20个。 当我买一个座位时,它会将*放在座位上,但当我买另一个座位时,它会将*移走,并将*放在新买的座位上。我怎样才能使它保存打印在每个座位上的*信息 全球的 /******************************************************************************
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
using namespace std;
int column2, row2,total = 0;
char ab[20][15];
char EMPTY = '#';
char FULL = '*';
int seat = 300;
int seat2 = 0;
int Quit = 1;
int choice;
int cost,answer,price;
void ShowSeats()
{
cout << "\tSeats" << endl;
cout << " 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n";
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 20; j++) {
ab[i][j] = EMPTY;
if (i % 2 && j == 14) // 0 is false, 1 is true
{
break;
}
if (i == row2 && j == column2) // assuming these numbers start from 0
{
ab[i][j] = '*';
}
}
}
for (int i = 0; i < 15; i++) {
cout << endl
<< "Row " << (i + 1);
for (int j = 0; j < 20; j++) {
cout << " " << ab[i][j];
}
}
}
int main()
{
while (true){
cout << "Please select the row you would like to sit in: ";
cin >> row2;
cout << "Please select the seat you would like to sit in: ";
cin >> column2;
cout<< "Enter the price";
cin >> price;
if (ab [row2] [column2] == '*')
{
cout << "Sorry that seat is sold-out, Please select a new seat.";
cout << endl;
}
else
{
cost = price;
cout << "That ticket costs: " << cost << endl;
cout << "Confirm Purchase? Enter (1 = YES)";
cin >> answer;
seat = seat - answer;
seat2 += answer;
if (answer == 1)
{
cout << "Your ticket purchase has been confirmed." << endl;
ab [row2][column2] = FULL;
total = total + cost;
cout << "Would you like to look at another seat? (1 = YES)";
cin>>Quit;
}
ShowSeats();
}
}}
/******************************************************************************
在线C++编译器。
代码,编译,运行和调试C++程序在线。
在这个编辑器中编写代码,然后按“运行”按钮编译并执行它。
*******************************************************************************/
#包括
使用名称空间std;
int column2,第2行,总计=0;
char ab[20][15];
char EMPTY='#';
字符完整='*';
内部座位=300;
int seat2=0;
int-Quit=1;
智力选择;
int成本、答案、价格;
空置座位()
{
cout在显示图表中,您正在执行以下操作:
ab[i][j] = EMPTY;
对于每个i
和j
。这意味着每次调用此函数时都会删除旧值
删除该行以避免覆盖已保存的座椅
要在请求任何用户输入之前初始化所有座椅,您可以在进入while
循环之前在main
中执行此操作:
for (int i = 0; i < 15; i++)
for (int j = 0; j < 20; j++)
ab[i][j] = EMPTY;
for(int i=0;i<15;i++)
对于(int j=0;j<20;j++)
ab[i][j]=空;
这个问题不完整。做一个。首先,ab
是在Show\u Chart
中声明的,但您也在其他函数中使用它?对不起,我忘了提到我已声明它为全局。您可以将ab
作为向量向量,并通过引用传递给Show\u Chart。这也会让你有不同的行和不同的长度。是的,我意识到了这一点,并且我已经删除了它,尽管它仍然没有在出售的座位上显示*。但是,它不允许我再次购买座位。那么,在你做出MRE之前,我们无法真正解决你的问题。显示你为你的功能提供了哪些输入,以及输出的不同之处你想要什么。我已经编辑了这个问题,你能不能再打开它。你的问题仍然不完整。没有人能用提供的代码复制你的结果。我已经制作了可复制的样本。你能打开问题并帮助我解决问题吗。