The problem is to make a seating chart. The user enters a row and column of the array to pick a seat. After the seat is picked, mark the location with an X. Repeat until all seats are taken, then end the program.
I made the array, was able to pick seats, and mark them with an x. I'm having trouble making a loop to repeat the process.
Here's what I have.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
//variables
const int row = 8, col = 10;
int r = 0, c = 0;
//1. Create an array
//title
cout << " Available Theater Seating\n\n";
//create while loop to check for available seats (non X elements)
string seats[row][col] =
{ {"$30 ", "$40 ", "$50 ", "$50 ", "$50 ", "$50 ", "$50 ", "$50 ", "$40 ", "$30"},
{"$20 ", "$30 ", "$30 ", "$40 ", "$50 ", "$50 ", "$40 ", "$30 ", "$30 ", "$20"},
{"$20 ", "$20 ", "$30 ", "$30 ", "$40 ", "$40 ", "$30 ", "$30 ", "$20 ", "$20"},
{"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
{"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
{"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
{"$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10"},
{"$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10"} };
//if (seats > X) {
//display array
//do {
for (int row = 7; row >= 0; row--)
{
for (int col = 0; col <= 9; col )
{
cout << seats[row][col];
}
cout << "\n";
}
//prompt for and get row/column
cout << "\nPick a seat by row and then column.\n\n";
cout << "Row: ";
cin >> r;
cout << "Column: ";
cin >> c;
seats[r - 1][c - 1] = " X ";
//repeat of display array to show the new x. Delete after making while loop
for (int row = 7; row >= 0; row--)
{
for (int col = 0; col <= 9; col )
{
cout << seats[row][col];
}
cout << "\n";
}
//} while (seats[][] !"X")
return 0;
}
This is what I get when I run it so far.
Available Theater Seating
$10 $10 $10 $10 $10 $10 $10 $10 $10 $10
$10 $10 $10 $10 $10 $10 $10 $10 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$20 $20 $30 $30 $40 $40 $30 $30 $20 $20
$20 $30 $30 $40 $50 $50 $40 $30 $30 $20
$30 $40 $50 $50 $50 $50 $50 $50 $40 $30
Pick a seat by row and then column.
Row: 3
Column: 4
$10 $10 $10 $10 $10 $10 $10 $10 $10 $10
$10 $10 $10 $10 $10 $10 $10 $10 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$10 $10 $20 $20 $20 $20 $20 $20 $10 $10
$20 $20 $30 X $40 $40 $30 $30 $20 $20
$20 $30 $30 $40 $50 $50 $40 $30 $30 $20
$30 $40 $50 $50 $50 $50 $50 $50 $40 $30
CodePudding user response:
Since you are looping through the whole array to print it out, you can count how many values are Xs. One thing that would make this easier is to not store the X as a string with the spaces in it. You can add the spaces later.
Add a variable to keep track of which seats are taken
const int totalSeats = rows * cols;
int seatsTaken = 0;
Then for each element in the array, count all the taken seats.
if (seats[row][col] == "X")
{
cout << " " << seats[row][col] << " ";
seatsTaken ;
}
else
{
cout << seats[row][col];
}
Then you will know all seats are taken if seatsTaken == totalSeats
CodePudding user response:
At Joe's suggestion, I added a separate counter for seats taken. When seats taken equal the total amount of seats, the program will end. Here's what I ended up with.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
//variables
const int row = 8, col = 10; //array size
int r = 0, c = 0; //input variables
const int totalSeats = row * col; //total available seats
int seatsTaken = 0; //count for seats taken
//1. Create an array
//title
cout << " Available Theater Seating\n\n";
//starting array
string seats[row][col] =
{ {"$30 ", "$40 ", "$50 ", "$50 ", "$50 ", "$50 ", "$50 ", "$50 ", "$40 ", "$30"},
{"$20 ", "$30 ", "$30 ", "$40 ", "$50 ", "$50 ", "$40 ", "$30 ", "$30 ", "$20"},
{"$20 ", "$20 ", "$30 ", "$30 ", "$40 ", "$40 ", "$30 ", "$30 ", "$20 ", "$20"},
{"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
{"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
{"$10 ", "$10 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$20 ", "$10 ", "$10"},
{"$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10"},
{"$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10 ", "$10"} };
//start loop
do {
for (int row = 7; row >= 0; row--) //2.count backwards to make the top of the array the bottom "front of theater"
{
for (int col = 0; col <= 9; col )
{
cout << seats[row][col]; //actual display of array
}
cout << "\n";
}
//3.,4.prompt for and get row/column
cout << "\nPick a seat by row and then column.\n\n";
cout << "Row: ";
cin >> r;
cout << "Column: ";
cin >> c;
cout << "\n";
//5.if seat is taken, give error code
if (seats[r - 1][c - 1] == " X ") {
cout << "Unavailable. Try again.\n";
}
//if seat is available, mark it with an x and increase count for seats taken
else {
seats[r - 1][c - 1] = " X ";
seatsTaken ;
}
//continue looping until seats taken = total seats
} while (seatsTaken != totalSeats);
//6.,7.when all seats are taken, give sold out message and end program
if (seatsTaken == totalSeats) {
cout << "The theater is sold out.";
return 0;
}
}