c++ - 如何在C++中预订剧院座位?

标签 c++ arrays

我是C++的初学者,现在还在学习。我的老师给我分配了一个项目,我正在尝试创建电影院的软件。我已经完成了基本选项和菜单。现在我被保留在座位上了。我设法使用数组创建了一个简单的预订系统,其中X是用户要预订的座位。如果再次预订或选择的座位不存在,它也会显示错误。现在我不知道如何显示“A1”,“C3”之类的特定座位号?

我只是一个初学者,我将在项目中使用特定的单独功能,但是我在此处发布的代码本身就是主要功能。

谢谢! PS:我知道我的代码并没有达到所需的效率,但是我只是在练习真实程序时才这样做的。

#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
int main()
{
    choose:
    string seat[]={"x1","y1","x2","y2","x3","y3","x4","y4","x5","y5","x6","y6","x7","y7","x8","y8","x9","y9","x10","y10","x11","y11","x12","y12","x13","y13","x14","y14","x15","y15","x16","y16","x17","y17","x18","y18","x19","y19","x20","y20","x21","y21","x22","y22","x23","y23","x24","y24","x25","y25","x26","y26","x27","y27","x28","y28","x29","y29","x30","y30","x31","y31","x32","y32","x33","y33","x34","y34","x35","y35","x36","y36"};
    int row,col;
    int input;
    char rows='1';
    char c;
    char book1;
    string arr[4][9]={{"[ ]","[ ]","[ ]","[ ]","[ ]","[ ]","[ ]","[ ]","[ ]"},{"[ ]","[ ]","[ ]","[ ]","[ ]","[ ]","[ ]","[ ]","[ ]"},
    {"[ ]","[ ]","[ ]","[ ]","[ ]","[ ]","[ ]","[ ]","[ ]"},{"[ ]","[ ]","[ ]","[ ]","[ ]","[ ]","[ ]","[ ]","[ ]"}};
    cout<<"\t\t            1   2   3   4   5   6   7   8   9"<<endl;
    for(int i=0; i<4; ++i)
    {
        cout<<"\t\t   Row "<<rows<<":  ";
        ++rows;
        for(int j=0; j<9; ++j)
            cout<<arr[i][j]<<" ";
        cout<<endl;
    }

    again:
    rows='1';
    for(input=1; input<37; input=input+2)
    {
        rows='1';
        cout<<endl<<"\t\t               Enter Row & Column: ";
        cin>>row>>col;
        if (row<=0 || col<=0)
        {
            cout<<endl<<"\t\t              Please Pick A Valid Seat"<<endl;
            Sleep(500);
            system("cls");
            goto choose;
        }
        --row;
        --col;
        if(arr[row][col]=="[X]")
        {
            cout<<endl<<"\t\t             Seat Not Available"<<endl;
            goto again;
        }
        arr[input][input+1]=arr[row][col];
        arr[row][col]="[X]";
        system("cls");
        cout<<endl;
        cout<<"\t\t            1   2   3   4   5   6   7   8   9"<<endl;
        for(int i=0; i<4; ++i)
        {
            cout<<"\t\t   Row "<<rows<<":  ";
            ++rows;
            for(int j=0; j<9; ++j)
               cout<<arr[i][j]<<" ";
            cout<<endl;
        }
        cout<<endl<<"\t\t                   Seat Booked!"<<endl;
        cout<<endl<<"\t\t        Do You Want To Book Another Seat?"<<endl;
        cout<<"\t\t          Press [Y] For Yes & [N] For No."<<endl;
        cout<<"\t\t                        ";

        cin>>c;
        if(c=='y' || c=='Y')
            continue;
        else if(c=='n' || c=='N')
        // Here I want the code to print seat number as A1 or C3 etc...
        break;
    }
}

最佳答案

如果要显示字母而不是数字,则可以使用这些字母的数组:

#include <iostream>

int main()
{
    static const char letters[] = "ABCDEFGHIJ";
    for (int i = 0; i < 10; i++) {
        std::cout << "#" << i << " is " << letters[i] << '\n';
    }
}

这将显示:
#0 is A
#1 is B
#2 is C
#3 is D
#4 is E
#5 is F
#6 is G
#7 is H
#8 is I
#9 is J

请注意,C++中的数组索引从0开始。
因此,如果要将行号1显示为A,将2显示为B,依此类推,则需要从行号中减去1,例如letters[row_number - 1]。但是最好学会像程序员一样思考第一件事在索引0,第二件事在索引1,依此类推。
#include <iostream>

void display(int row, int seat)
{
    static const char letters[] = "ABCDEFGHIJ";
    std::cout << letters[row - 1] << seat;
}

int main()
{
    for (int i = 1; i < 11; i++) {
        std::cout << "#" << i << " is ";
        display(i, i);
        std::cout << '\n';
    }
}

关于c++ - 如何在C++中预订剧院座位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59478244/

相关文章:

c++ - GetQueuedCompletionStatus 过早退出(或者我认为)

c++ - 将 unique_ptr 移入 lambda 时,为什么不能调用 reset?

c++ - 如何变换骨骼动画的顶点?

arrays - 检查字典中的数组并返回 UIImage 但它返回相同图像的两倍

java - 无法计算折扣

java - String(byte[]) 是否创建字节数组的深拷贝?

c++ - 在 Windows 和 Mac 上发送 http 包的最简单方法

javascript - 为什么不能用点符号访问 javascript 数组元素?

c# - 将字符串数组排序为整数,但可以有非整数值

c++ - 模板函数在读取时返回错误值