问题是制作座位表。用户输入数组的一行和一列来选择座位。选择座位后,用X标记位置。重复直到所有座位都被占满,然后结束程序。
我创建了数组,能够选择座位,并用x标记它们。我在创建循环以重复该过程时遇到了麻烦。
这是我的资料。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
//variables
const int row = 8, col = 10;
int r = 0, c = 0;
//1. Create an array
//title
cout << " Available Theater Seating\n\n";
//create while loop to check for available seats (non X elements)
string seats[row][col] =
{ {"$30 ", "$40 ", "$50 ", "$50 ", "$50 ", "$50 ", "$50 ", "$50 ", "$40 ", "$30"},
{"$20 ", "$30 ", "$30 ", "$40 ", "$50 ", "$50 ", "$40 ", "$30 ", "$30 ", "$20"},
{"$20 ", "$20 ", "$30 ", "$30 ", "$40 ", "$40 ", "$30 ", "$30 ", "$20 ", "$20"},
{"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
{"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
{"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
{"$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10"},
{"$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10"} };
//if (seats > X) {
//display array
//do {
for (int row = 7; row >= 0; row--)
{
for (int col = 0; col <= 9; col++)
{
cout << seats[row][col];
}
cout << "\n";
}
//prompt for and get row/column
cout << "\nPick a seat by row and then column.\n\n";
cout << "Row: ";
cin >> r;
cout << "Column: ";
cin >> c;
seats[r - 1][c - 1] = " X ";
//repeat of display array to show the new x. Delete after making while loop
for (int row = 7; row >= 0; row--)
{
for (int col = 0; col <= 9; col++)
{
cout << seats[row][col];
}
cout << "\n";
}
//} while (seats[][] !"X")
return 0;
}
这是我运行到目前为止得到的结果。
Available Theater Seating
$10 $10 $10 $10 $10 $10 $10 $10 $10 $10
$10 $10 $10 $10 $10 $10 $10 $10 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$20 $20 $30 $30 $40 $40 $30 $30 $20 $20
$20 $30 $30 $40 $50 $50 $40 $30 $30 $20
$30 $40 $50 $50 $50 $50 $50 $50 $40 $30
Pick a seat by row and then column.
Row: 3
Column: 4
$10 $10 $10 $10 $10 $10 $10 $10 $10 $10
$10 $10 $10 $10 $10 $10 $10 $10 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$20 $20 $30 X $40 $40 $30 $30 $20 $20
$20 $30 $30 $40 $50 $50 $40 $30 $30 $20
$30 $40 $50 $50 $50 $50 $50 $50 $40 $30
2条答案
按热度按时间bvn4nwqk1#
在乔的建议下,我添加了一个单独的座位计数器。当座位数等于总座位数时,程序将结束。以下是我的结果。
eqqqjvef2#
由于要循环遍历整个数组并打印出来,因此可以计算出有多少个值是X。有一件事会使计算更容易,那就是不要将X存储为包含空格的字符串,可以稍后添加空格。
添加一个变量以跟踪哪些座位已被占用
然后对于数组中的每个元素,计算所有占用的座位。
如果
seatsTaken == totalSeats
,则您将知道所有座位都已占用