二维阵列座位图C ++

时间:2017-05-07 20:31:17

标签: c++

我正在学习c ++,我需要制作一个座位表程序。这不是我的代码,因为我正在研究它以了解每个部分的工作原理。我对座位表感兴趣。我已经尝试了许多不同的东西来尝试自己解决这个问题,我已经接近了,在右栏中有完整(*)标记,但它从来没有在正确的行中。 如果我选择座位#1和行#1,它会在座位和行#2的图表上做出标记。  同样,我没有使用这个代码,我只是在测试时看得更清楚,看看它们是如何工作的。 这是代码

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

int Show_Menu();
void Show_Chart();
const char FULL = '*';
const char EMPTY = '#';
const int rows = 11;
const int columns = 10;
char map[rows][columns];
double price;
int total = 0;
int seat = 90;
int seat2 = 0;
int Quit = 1;

int main()
{
    const int Num_Rows = 11;
    double price[Num_Rows];
    int row2, column2, cost;
    int answer;

    //I have this blocked out as I am testing the chart only
/*  cout << "Please enter price for each row." << endl;

    for (int count = 0; count < rows; count++)
    {
        cout << "Row # " << (count + 1) << ": ";
        cin >> price[count];

    }
*/  
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
            map[i][j] = EMPTY;
    }
    int choice;
    do
    {
        choice = Show_Menu();
        switch (choice)
        {
        case 1:
            cout << "View Seat Prices\n\n";

            for (int count = 0; count < rows; count++)
            {
                cout << "The price for row " << (count + 1) << ": ";
                cout << price[count] << endl;
            }
            break;
        case 2:
            cout << "Purchase a Ticket\n\n";
            do
            {
                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;
                if (map[row2][column2] == FULL)
                {
                    cout << "Sorry that seat is sold-out, Please select a new 
                             seat.";
                    cout << endl;
                }
            else
            {
                cost = price[row2] + 0;
                total = total + cost;
                cout << "That ticket costs: " << cost << endl;
                cout << "Confirm Purchase? Enter (1 = YES / 2 = NO)";
                cin >> answer;

            if (answer == 1)
            {
                cout << "Your ticket purchase has been confirmed."
                     << endl;
                map[row2][column2] = FULL;
            }
            else if (answer == 2)
            {
                cout << "Would you like to look at another seat? (1 =                    
                     YES / 2 = NO)";
                cout << endl;
                cin >> Quit;
                    }

                    cout << "Would you like to look at another seat?(1 = YES / 2 
                            = NO)";
                    cin >> Quit;
                }
            } while (Quit == 1);

            break;
        case 3:
            cout << "View Available Seats\n\n";
            Show_Chart();
            break;
        case 4:
            cout << "Total ticket sales: " << total << ".\n\n";
            break;
        case 5:
            cout << "quit\n";
            break;
        default: cout << "Error input\n";
        }
    } while (choice != 5);
    return 0;
}


int Show_Menu()
{
    int MenuChoice;
    cout << endl << endl;
    cout << " \tMAIN MENU\n";
    cout << " 1. View Seat Prices.\n";
    cout << " 2. Purchase a Ticket.\n";
    cout << " 3. View Available Seats.\n";
    cout << " 4. View Ticket Sales.\n";
    cout << " 5. Quit the program.\n";
    cout << "_____________________\n\n";
    cout << "Please enter your choice: ";
    cin >> MenuChoice;
    cout << endl << endl;
    return MenuChoice;
}

void Show_Chart()
{

    cout << "Seats 1 2 3 4 5 6 7 8 9 ";
    for (int row = 0; row < 10; row++)//rows
    {
        cout << endl << "Row " << (row + 1);

        for (int columns = 0; columns < 9; columns++)
        {
            cout << " " << map[row][columns];
        }
    }
    cout << endl;
}

1 个答案:

答案 0 :(得分:0)

  

如果我选择#1座位和#1行,则会在图表上显示标记   座位和排#2 ......

问题是C中的数组以索引0开头,因此最左边的座位有索引0,而不是1。因此,如果您将1输入row2并写入map[row2][column2] = FULL,则实际上会标记第二个座位。

一个简单的解决方法就是写map[row2-1][column2-1];但请确保用户不得输入值0