Hello again, I'm having trouble with a program that I have to write involving classes and arrays. Essentially i have to create a class Theater, which I am not sure how to do. The theater has 15 rows with 30 seats per row, and each row is assigned a price for each seat, inputted from a text file. I cannot seem to get the 2d array to work properly. The user should be able to purchase a specific seat using the inputted prices, which keeps a running total of seats purchased. The output should show each row and seat and put a * if the seat is purchased and a # if the seat is available.

This is what I have so far, please let me know if it is to sloppy and I will do my best to make it cleaner, sorry.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib> 
using namespace std;

void menu();
void purchase();
void total_value();
void total_number();
void seats_row();
void seats_all();
void seating_chart();

void get_price()
{
    int i;
    ifstream fin;
    fin.open("seats.txt");
    int price[15];
    for(i=1; i<16; i++)
    {
        fin >> price[i];
        cout << "Row " << i << " is $" << price[i] << endl;
    }
}

void purchase()
{
    int seat;
    int row;
    int total;
    do
    {
        do
        {
            cout << "-------- Purchase Seats --------" << endl
                 << "Enter the row and seat number desired." << endl
                 << "There are 15 rows and 30 seats per row." << endl
                 << "Enter -1 for the Row Number When You are Done." << endl;
            cout << "Row Number: ";
            cin >> row;
            if(row == -1)
            {
                cin.sync();
                menu();
            }
            cout << "Seat Number: ";
            cin >> seat;
            cout << endl;
        }while(row > 0);
        if(row < 1 || row > 15 || seat < 1 || seat > 30)
            cout << "You must enter an appropriate value.\n\n";
    }while(row < 1 || row > 15 || seat < 1 || seat > 30);
}

void total_value()
{
    get_price();
}

void total_number()
{
}

void seats_row()
{
}

void seats_all()
{
}

void seating_chart()
{
    int i,j;
    double theater[15][30];
    for(i=0; i<15; i++)
    {
        for(j=0; j<30; j++)
        {
            cin >> theater[i][j];
            cout << theater[i][j];
        }
    }
    
}

// MENU

void menu()
{
    int choice;
    do
    {
        cout << "Choose One of the Following." << endl
             << "(1) Purchase Tickets." << endl
             << "(2) Display Total Dollar Value of All Tickets Sold." << endl
             << "(3) Display Total Number of Tickets Sold." << endl
             << "(4) Display the Number of Seats Available in Each Row." << endl
             << "(5) Display the Number of Seats Available in the Entire Auditorium." << endl
             << "(6) Display the Current Seating Chart." << endl
             << "(7) Exit the System." << endl
             << "Choice: ";
        cin >> choice;
        if(choice < 1 || choice > 7)
            cout << "You must enter a number 1 through 7." << endl;;
        cout << endl;
    }while(choice < 1 || choice > 7);

    if(choice == 1)
        purchase();
    if(choice == 2)
        total_value();
    if(choice == 3)
        total_number();
    if(choice == 4)
        seats_row();
    if(choice == 5)
        seats_all();
    if(choice == 6)
        seating_chart();
    if(choice == 7)
        exit(1);

}

// MAIN FUNCTION

int main()
{
    menu();
    return 0;
}

I have helped you out by writing what I think you want and put it into a "cleaner" format (could be more clean) and divided it into seperate files for structure. (file allinone.cpp is the whole thing in one file if this is for school and they want it in one)

I did my best to comment lines to show you what is going on.

I attached all the files.

Attachments
#include <iostream>
#include <fstream>

using namespace std;

struct SEAT //seat structure
{
	double price; //set later to a value from your input text file
	char status; //either '*' or '#'
};

class THEATER
{
	SEAT seats[15][30]; //the seats in the theater
	double ticketsSold, ticketsSoldDollars; //values for tickets sold and how much money the tickets were worth
	
	public:
		
	THEATER(){}; //default constructor
	THEATER(const char* inputFile); //new constructor
	
	void menu(); //main menu
	
	void getSeatsAvailable(const char* sel); //says how many seats are available overall or by row (based on sel)
	void getTicketsSold(); //outputs ticketsSold
	void getTicketsSoldDollars(); //outputs tickestSoldDollars
	
	void display(); //displays all the seats in the theater
	void purchase(); //asks user what seat they want to buy
};

THEATER::THEATER(const char* inputFile)
{
	ticketsSold = 0, ticketsSoldDollars = 0;
	ifstream in;
	double price;
	
	in.open(inputFile);
	
	for( int i = 0; i < 15; i++ )
	{
		in >> price; //get price from the text file
		for( int c = 0; c < 30; c++ )
		{
			seats[i][c].price = price; //set the price to each seat in the row
			seats[i][c].status = '#'; //set each seat to available
		}
	}
	in.close();
	menu(); //starts the menu function
}

void THEATER::menu() //main part of program
{
	int option; //option you choose
	bool valid; //valid input
	
	cout << "Choose One of the Following." << endl
		 << "(1) Purchase Tickets." << endl
		 << "(2) Display Total Dollar Value of All Tickets Sold." << endl
		 << "(3) Display Total Number of Tickets Sold." << endl
		 << "(4) Display the Number of Seats Available in Each Row." << endl
		 << "(5) Display the Number of Seats Available in the Entire Auditorium." << endl
		 << "(6) Display the Current Seating Chart." << endl
		 << "(7) Exit the System." << endl
         << "Choice: ";
    do
    {
    	cin >> option; //get option
    	
		valid = true; //set valid to true for
		
    	switch(option)
    	{
			case 1:
				purchase(); //asks the user what seats they would like to buy
				break;
			case 2:
				getTicketsSoldDollars(); //returns the ticketsSoldDollars variable
				break;
			case 3:
				getTicketsSold(); //returns the ticketsSold variable
				break;
			case 4:
				getSeatsAvailable("pick row"); //let the user pick the row (will prompt)
				break;
			case 5:
				getSeatsAvailable("all seats"); //outputs number of seats available for all rows
				break;
			case 6:
				display(); //displays all seats ('#' if available and '*' if taken)
				break;
			case 7:
				return; //sends the program back to main where it will end
				break;
			default:
				valid = false; //if is not one of the above numbers make valid false
				break;
		}
	}while(!valid); //if valid is false reloop
}

void THEATER::getSeatsAvailable(const char* sel)
{
	int numberOfSeats = 0, row;
	bool valid; //if input is valid
	
	if( sel == "all seats" )
	{
		for( int i = 0; i < 15; i++ ) //loop for row
			for( int c = 0; c < 30; c++ ) //loop for col
				if( seats[i][c].status == '#' ) //checks if is available
					numberOfSeats++; //add one to counter
		cout << "The number of seats avaliable is: ";
	}
	
	if( sel == "pick row" )
	{
		cout << "Choose a row: ";
		do
		{
			valid = false;
			cin >> row;
			
			if( row = 0 && row <= 15 ) //if within range valid is true
				valid = true;
				
		}while(!valid); //if input is bad reloop
		
		for( int i = 0; i < 30; i++ ) //loop for col
			if( seats[row-1][i].status == '#' ) //checks row from user input to see what seats are available
				numberOfSeats++; //add one to counter
		cout << "The number of seats avaliable in row " << row << " is: ";
	}
	cout << numberOfSeats << endl << endl; //outputs the seats
	
	menu(); //back to main menu
}

void THEATER::getTicketsSold()
{
	cout << "Total number of tickets sold: " << ticketsSold << endl << endl;
	menu(); //back to main menu
}

void THEATER::getTicketsSoldDollars()
{
	cout << "Total dollar value of tickets sold: " << ticketsSoldDollars << endl << endl;
	menu(); //back to main menu
}

void THEATER::display()
{
	cout << endl;
	for( int i = 0; i < 15; i++ )
	{
		for( int c = 0; c < 30; c++ )
		{
			cout << seats[i][c].status << " "; //outputs the status for each seat
		}
		cout << endl;
	}
	cout << endl;
	menu(); //back to main menu
}


void THEATER::purchase()
{
	int row, col, seatsTaken = 0;
	char option;
	bool valid;
	cout << endl << "What row would you like to sit in? ";
	
	do
	{
		valid = false;
		cin >> row;
		
		if( row > 0 && row <= 15 )
			for( int i = 0; i < 30; i++ )
				if( seats[row-1][i].status == '#' ) //if atleast one seat in the row is not taken valid is true
					valid = true;
				else
					seatsTaken++;
	
	if( seatsTaken == 15 )
		cout << "Sorry that row is full." << endl;
	
	}while(!valid);
	
	cout << endl << "What seat would you like to have in that row? ";
	
	do
	{
		valid = false;
		cin >> col;
		
		if( col > 0 && col <= 30 )
			if( seats[row-1][col-1].status == '#' ) //if seat is not taken
					valid = true;
				else
					cout << "Sorry that seat is taken." << endl;
	
	}while(!valid);
	
	cout << "This seat costs $" << seats[row-1][col-1].price << ". Would you like to purchase it?(y or n) "; //shows price of seat
	cin >> option;
	
	if( option == 'y' || option == 'Y' )
	{
		ticketsSold++; //add one to ticket counter
		ticketsSoldDollars += seats[row-1][col-1].price; //add price of ticket to total tickestSoldDollars
		seats[row-1][col-1].status = '*'; //sets the seat to unavailable
	}
	else
	{
		cout << "Would you still like to buy another seat?(y or n) "; //if you dont want the seat because it costs too much
		cin >> option;
		
		if( option ==  'y' || option == 'Y' )
			purchase();
		else
			menu();
	}
	cout << "Would you like to buy another seat?(y or n) "; //if you bought a seat and you want might want another
	cin >> option;
	
	if( option == 'y' || option == 'Y' )
		purchase();
	cout << endl;
	
	menu();//back to main menu
}

int main()
{
	THEATER theater("input.txt"); //sets the values of the rows to those inside "input.txt" (change to want your file is)
	
	system("PAUSE");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef LIB_H
#define LIB_H

#include <iostream>
#include <fstream>

using namespace std;

#include "theater.h"


#endif
#include "lib.h"

int main()
{
	THEATER theater("input.txt"); //sets the values of the rows to those inside "input.txt" (change to want your file is)
	
	system("PAUSE");
	return 0;
}
#include "lib.h"

THEATER::THEATER(const char* inputFile)
{
	ticketsSold = 0, ticketsSoldDollars = 0;
	ifstream in;
	double price;
	
	in.open(inputFile);
	
	for( int i = 0; i < 15; i++ )
	{
		in >> price; //get price from the text file
		for( int c = 0; c < 30; c++ )
		{
			seats[i][c].price = price; //set the price to each seat in the row
			seats[i][c].status = '#'; //set each seat to available
		}
	}
	in.close();
	menu(); //starts the menu function
}

void THEATER::menu() //main part of program
{
	int option; //option you choose
	bool valid; //valid input
	
	cout << "Choose One of the Following." << endl
		 << "(1) Purchase Tickets." << endl
		 << "(2) Display Total Dollar Value of All Tickets Sold." << endl
		 << "(3) Display Total Number of Tickets Sold." << endl
		 << "(4) Display the Number of Seats Available in Each Row." << endl
		 << "(5) Display the Number of Seats Available in the Entire Auditorium." << endl
		 << "(6) Display the Current Seating Chart." << endl
		 << "(7) Exit the System." << endl
         << "Choice: ";
    do
    {
    	cin >> option; //get option
    	
		valid = true; //set valid to true for
		
    	switch(option)
    	{
			case 1:
				purchase(); //asks the user what seats they would like to buy
				break;
			case 2:
				getTicketsSoldDollars(); //returns the ticketsSoldDollars variable
				break;
			case 3:
				getTicketsSold(); //returns the ticketsSold variable
				break;
			case 4:
				getSeatsAvailable("pick row"); //let the user pick the row (will prompt)
				break;
			case 5:
				getSeatsAvailable("all seats"); //outputs number of seats available for all rows
				break;
			case 6:
				display(); //displays all seats ('#' if available and '*' if taken)
				break;
			case 7:
				return; //sends the program back to main where it will end
				break;
			default:
				valid = false; //if is not one of the above numbers make valid false
				break;
		}
	}while(!valid); //if valid is false reloop
}

void THEATER::getSeatsAvailable(const char* sel)
{
	int numberOfSeats = 0, row;
	bool valid; //if input is valid
	
	if( sel == "all seats" )
	{
		for( int i = 0; i < 15; i++ ) //loop for row
			for( int c = 0; c < 30; c++ ) //loop for col
				if( seats[i][c].status == '#' ) //checks if is available
					numberOfSeats++; //add one to counter
		cout << "The number of seats avaliable is: ";
	}
	
	if( sel == "pick row" )
	{
		cout << "Choose a row: ";
		do
		{
			valid = false;
			cin >> row;
			
			if( row = 0 && row <= 15 ) //if within range valid is true
				valid = true;
				
		}while(!valid); //if input is bad reloop
		
		for( int i = 0; i < 30; i++ ) //loop for col
			if( seats[row-1][i].status == '#' ) //checks row from user input to see what seats are available
				numberOfSeats++; //add one to counter
		cout << "The number of seats avaliable in row " << row << " is: ";
	}
	cout << numberOfSeats << endl << endl; //outputs the seats
	
	menu(); //back to main menu
}

void THEATER::getTicketsSold()
{
	cout << "Total number of tickets sold: " << ticketsSold << endl << endl;
	menu(); //back to main menu
}

void THEATER::getTicketsSoldDollars()
{
	cout << "Total dollar value of tickets sold: " << ticketsSoldDollars << endl << endl;
	menu(); //back to main menu
}

void THEATER::display()
{
	cout << endl;
	for( int i = 0; i < 15; i++ )
	{
		for( int c = 0; c < 30; c++ )
		{
			cout << seats[i][c].status << " "; //outputs the status for each seat
		}
		cout << endl;
	}
	cout << endl;
	menu(); //back to main menu
}


void THEATER::purchase()
{
	int row, col, seatsTaken = 0;
	char option;
	bool valid;
	cout << endl << "What row would you like to sit in? ";
	
	do
	{
		valid = false;
		cin >> row;
		
		if( row > 0 && row <= 15 )
			for( int i = 0; i < 30; i++ )
				if( seats[row-1][i].status == '#' ) //if atleast one seat in the row is not taken valid is true
					valid = true;
				else
					seatsTaken++;
	
	if( seatsTaken == 15 )
		cout << "Sorry that row is full." << endl;
	
	}while(!valid);
	
	cout << endl << "What seat would you like to have in that row? ";
	
	do
	{
		valid = false;
		cin >> col;
		
		if( col > 0 && col <= 30 )
			if( seats[row-1][col-1].status == '#' ) //if seat is not taken
					valid = true;
				else
					cout << "Sorry that seat is taken." << endl;
	
	}while(!valid);
	
	cout << "This seat costs $" << seats[row-1][col-1].price << ". Would you like to purchase it?(y or n) "; //shows price of seat
	cin >> option;
	
	if( option == 'y' || option == 'Y' )
	{
		ticketsSold++; //add one to ticket counter
		ticketsSoldDollars += seats[row-1][col-1].price; //add price of ticket to total tickestSoldDollars
		seats[row-1][col-1].status = '*'; //sets the seat to unavailable
	}
	else
	{
		cout << "Would you still like to buy another seat?(y or n) "; //if you dont want the seat because it costs too much
		cin >> option;
		
		if( option ==  'y' || option == 'Y' )
			purchase();
		else
			menu();
	}
	cout << "Would you like to buy another seat?(y or n) "; //if you bought a seat and you want might want another
	cin >> option;
	
	if( option == 'y' || option == 'Y' )
		purchase();
	cout << endl;
	
	menu();//back to main menu
}
#ifndef THEATER_H
#define THEATER_H

struct SEAT //seat structure
{
	double price; //set later to a value from your input text file
	char status; //either '*' or '#'
};

class THEATER
{
	SEAT seats[15][30]; //the seats in the theater
	double ticketsSold, ticketsSoldDollars; //values for tickets sold and how much money the tickets were worth
	
	public:
		
	THEATER(){}; //default constructor
	THEATER(const char* inputFile); //new constructor
	
	void menu(); //main menu
	
	void getSeatsAvailable(const char* sel); //says how many seats are available overall or by row (based on sel)
	void getTicketsSold(); //outputs ticketsSold
	void getTicketsSoldDollars(); //outputs tickestSoldDollars
	
	void display(); //displays all the seats in the theater
	void purchase(); //asks user what seat they want to buy
};

#endif

wow thanks a lot, im fairly sure i can work through it now, much appreciated.

I see you just took the allinone.cpp if you actually want to learn some form of structure for coding bigger projects I suggest you look at the divided files and then look at the single. I find looking at something with 500+ lines a little overwhelming when you are just starting out.

Yeah that may help, I'm just using them as guidelines and re-writing what i originally had. Thanks

This question has already been answered. Start a new discussion instead.