Need help with code

Hello i am trying to write a programm for my college c++ class. This class is the second level and i took the first level two years ago due to just transferring to different school/major. My project is to write a program for a small theater that will show what seats are available and compute ticket purchases. I am having trouble with arrays and functions. In my code below, i was able to write a menu function and have it displayed. That works. I then wrote a function that makes all the values of the 2 dimensional array(seating chart) a "a". Then the function displays the 2 dimensional array to the screen. I wrote this code in a seperate program in the main function to see if it worked. When it runs, it displays all the "a"'s but it does accross the whole screen instead of in a 15 row 30 column chart. My second problem, is im not very familier with functions. I cant get the function to display the chart (even if the chart displays wrong) to display when the user inputs the number 2. Really need help. Once i have it explained and shown to me i will understand it better.


//Author: Steven Cortright
//Course: CSC 136
//Date: Spring 2013
//Purpose: Develop a program that will allow a theater to sell tickets for performances

#include <cstdlib>
#include <string>
#include <iostream>
//#include <iomainip>
#include "search.h"
#include <fstream>
using namespace std;

const int numSeats = 30;
const int numRows = 15;


void availableSeating (char seating[numRows][numSeats]);
int menu();


int main()
{

int choice = 0;
choice = menu();
switch (choice)
{
case 1:
cout << "You picked number 1.";
break;
case 2:
availableSeating();
break;
case 3:
cout << "You picked number 3.";
break;
case 4:
cout << "You picked number 4.";
break;

}

}
int menu()
{
int pick;
cout << endl;
cout << "1. Process a Customer" << endl;
cout << "2. Print a seating chart" << endl;
cout << "3. Report total tickets sold and total price" << endl;
cout << "4. Quit" << endl;
cout << "Choice: ";
cin >> pick;
cout << endl;
return pick;
}

void availableSeating (char seating[numRows][numSeats])
{
char input = 'a';
for(int row = 0; row < 15; row ++)
{
for (int col = 0; col < 30; col ++)
{
seating[row][col] = input;
}
}
for(int row = 0; row < 15; row ++)
{
for (int col = 0; col < 30; col ++)
{
cout << seating[row][col];
}
}
}
For your first problem, add a cout << endl; statement after your inner for loop while outputting the array.

For your second problem, you're not passing anything in to the availableSeating(...) function. You need to make an array in main, and then populate it (by moving the first set of for loops in your availableSeating(...) function to main).
First of all: Where you DECLARE array for seating (in search.h?)? What kind of array that would be? [bool would be nice (true=free seat, false = preserved seat)]

After you fixed that: (printing seats)
pseudo code:
1
2
3
4
5
6
for each line of seats:
  for each column of seats:
    print out current seat // reserved/free
  end
  endline
end


look closely on availableSeating()-function. Each time you enter there you make sure that all "seating[][]" cells are set to 'a'. After that you print all the a's.
Last edited on
when i add the cout <<endl to the inner for loop, it displays all the values in a single column?

I got the values to pass back to the main to display when the option is selected. thanks
I said after your inner loop, so outside of it, right before the end of the outer one. Like this:
1
2
3
4
5
6
7
8
for(int row = 0; row < 15; row ++)
{
for (int col = 0; col < 30; col ++)
{
cout << seating[row][col];
}
cout << endl; //here
}


FYI you should use code tags in the future (just highlight your code and hit the "<>" button under Format). Otherwise it's a pain to read.
Last edited on
Lets see sample program i made. I made free seats as global for a purpose. This just shows how functioning program helps readability.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

using namespace std;

const int numCols = 30;
const int numRows = 15;

bool seats[numCols][numRows];

void print_chart();
void init();

int main()
{
	init(); //make sure all seats are free
	print_chart(); //Print current seat configuration
	return 0;
}

void print_chart()
{
	for (int i=0; i<numCols; i++) {
		for (int j=0; j<numRows; j++)
			cout << seats[i][j]; // remember number 1 printed means free seat; with if statelement you can make it to be (F)ree or (R)esevred
	cout << endl;
	}
}

void init()
{
	for (int i=0; i<numCols; i++) {
		for (int j=0; j<numRows; j++) {
			seats[i][j]=true; // true is free seat
		}
	}
}


little note: i did make for loops accid.... on purpose to show you.... ehm... sideways view of seatings
it worked lol. i realize now why it wouldn;t work with have the endl where i had it. Having it outside like that allows the loop to run through for each row and then return instead of returning after every element. thank you for the help
Topic archived. No new replies allowed.