复杂的C++作业

Complicated C++ homework

本文关键字:作业 C++ 复杂      更新时间:2023-10-16

所以我正在处理剧院座位问题。程序的输出没有按应有的方式排列。我需要帮助把#和*排成合适的列。同样,按Q退出也不起作用。此外,我需要弄清楚如何从文件中读取座位表信息。如果包含座位信息的文件还不存在,则表示所有座位都是空的每当节目结束时,座位表信息都应存储在此文件中。任何其他提示也会有所帮助。这是我到目前为止的代码:

#include "stdafx.h"
# include <iostream>
# include <iomanip>
using namespace std;
void seats( double [] , int);
void mapSeats();
char movieMenu(char);
int main()
{
    const int rowNum = (15.0);
    double rowValue[rowNum]; //array to hold row pices
    char selection;
    int row2, col2;
    const char TAKEN = '#';//seats taken
    const char EMPTY = '*';//seats free
    const int row = 15;//number of rows
    const int col = 20;//number of col
    char map[row][col];//array to hold seat chart 

for(int i= 0;i<row;i++)//initiating array 
{
    for (int j=0;j<col;j++)
    {
        map[i][j]=EMPTY;
    }
}
mapSeats();
seats(rowValue, rowNum);//ask user to enter price of each row
cout << endl;
do
    {
        cout << "MOVIE THEATER MENU" << endl;
        cout << "------------------" << endl;
        cout << "1) Sell a ticket" << endl;
        cout << "Q) Quit program" << endl;
        cout << "Please make a selection: ";
        cin >> selection;
        if(selection =='1')
        {
            cout << "Please enter a row number and a seat number for the ticket: " ;
            cout << "Row # :" ;
            cin >> row2;
            cout << endl;
            cout << "Seat # :" ;
            cin >> col2;
            cout << endl;
            // Check if seat is free
        if(map[row2][col2] == TAKEN) {
            cout << "This seat is taken! Try another one. n";
            continue; // start the loop again
                     }
            else // and if it is - sell the ticket
            map[row2][col2]=TAKEN;
        // Add the next loop to immediately see the effects:
            for (int i = 0; i < row; i++){
                for(int j = 0; j < col; j++){
                cout << map[i][j];
                }
cout << endl;
    }
        }
        else if(selection =='q'||selection=='Q')
        {
            cout << "Thank you for using the program." << endl;
        }
        else if(selection != '1' || selection !='q' || selection !='Q')
        {
            cout << "Invalid selection." << endl;
        }
    }while(selection != '1' || selection !='q' || selection !='Q');

system("pause");
return 0;
    }

    void seats(double rowPrice[], int row)
    {
cout << "Please enter a ticket price for each row." << endl;
for(int i = 0 ; i < row; i++)
{
    cout << "Row # " << i+1 << ": " ;
    cin >> rowPrice[i]; 
}
    }
    void mapSeats()
    {
    const char TAKEN = '#';//seats taken
    const char EMPTY = '*';//seats free
    const int rw=20;
    const int cl=15;
    cout << "Seats " ;
    for(int k = 0 ; k <20;k++) //loop to display nums 0 to 19
    {
    cout << fixed<< setw(2) << " " << k ;
    }
for(int i=0;i<rw;i++)//making array display what's in it
{
    cout << endl<< "Row " << i;
    for(int j=0;j<cl;j++)
    {
        cout << fixed<< setw(2) << "" << EMPTY;
    }
}
cout << endl;
    }

Q退出不起作用。你的逻辑是错误的

do
{
    ...
} while (selection !='q' && selection !='Q');

选择不是'q'时继续,选择不是'q'时继续。对于新手来说,混淆"或"answers"和"是很常见的。