My goal for this code is for it to show the current seat numbers, take the seat number to reserve from the user, set that seat number value to 0, then print it back to the user and make the code loop. I am now stuck at displaying when the seat is taken. once two or more seats have been reserved, it prints more seat is taken messages.
Here's my code:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "_pause.h"
using namespace std;
int main() {
int s[5][7] = {
{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}
};
int i, j, k, l, m;
do {
cout << "Seats Available:" << endl;
cout << endl;
for (i = 0; i < 5; i ){
for (j = 0; j < 7; j ){
cout << s[i][j] << "\t";
}
cout << endl;
}
cout << "Enter Seat Number: ";
cin >> s[i][j];
cout << endl;
m - 1;
if (s[i][j] > 35){
cout << "Error, Invalid Seat number." << endl;
cout << endl;
} else {
for (k = 0; k < i; k ){
for (l = 0; l < j; l ){
if (s[i][j] == s[k][l]){
s[k][l] = 0;
cout << "Seat Successfully Reserved." << endl;
cout << endl;
} else if (s[k][l] == 0){
cout << "Seat Taken." << endl;
cout << endl;
}
}
}
}
} while (m > 1);
system ("pause");
return 0;
}
CodePudding user response:
This is probably what you want:
#include <iostream>
#include <string>
#include <array>
static constexpr std::size_t rowCount { 5 };
static constexpr std::size_t colCount { 7 };
void initializeSeats( std::array< std::array<std::size_t, colCount>, rowCount>& seats )
{
std::size_t idx { };
for ( auto& row : seats )
{
for ( auto& col : row )
{
col = idx;
}
}
}
void printSeats( const std::array< std::array<std::size_t, colCount>, rowCount>& seats )
{
std::cout << "Seats Available:" << '\n';
for ( const auto& row : seats )
{
for ( const auto& col : row )
{
std::cout << col << '\t';
}
std::cout << '\n';
}
}
bool reserveSeat( std::array< std::array<std::size_t, colCount>, rowCount>& seats )
{
std::cout << "Enter the Seat Number: ";
std::string selectedSeat { };
std::cin >> selectedSeat;
const std::size_t seatNum { std::stoull( selectedSeat ) };
bool isReserveSuccessful { };
for ( auto& row : seats )
{
for ( auto& col : row )
{
if ( col == seatNum )
{
col = 0;
return isReserveSuccessful = true;
}
}
}
return isReserveSuccessful = false;
}
void printReservationMsg( const bool isReserveSuccessful )
{
( isReserveSuccessful ) ? std::cout << "Seat Successfully Reserved!\n" :
std::cout << "Seat Reservation Unsuccessful!\n";
}
int main( )
{
std::array< std::array<std::size_t, colCount>, rowCount> seats { };
initializeSeats( seats );
char isExiting { };
do
{
printSeats( seats );
const bool isReserveSuccessful { reserveSeat( seats ) };
printReservationMsg( isReserveSuccessful );
std::cout << "\nEnter C to continue, E to end the program: ";
std::cin >> isExiting;
std::cin.ignore( );
} while ( isExiting == 'C' || isExiting == 'c' );
}
Sample input/output:
Seats Available:
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
Enter the Seat Number: 3
Seat Successfully Reserved!
Enter C to continue, E to end the program: c
Seats Available:
1 2 0 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
Enter the Seat Number: 32
Seat Successfully Reserved!
Enter C to continue, E to end the program: C
Seats Available:
1 2 0 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 0 33 34 35
Enter the Seat Number: 32
Seat Reservation Unsuccessful!
Enter C to continue, E to end the program: e
CodePudding user response:
As noted in the comments, cin >> s[i][j]; after the loop will make your program have undefined behavior since it writes out of bounds and you should use a separate variable for the input.
A correction of both could look like this:
#include <iostream>
// Define constants with names instead of using 5 and 7 everywhere:
constexpr unsigned Rows = 5;
constexpr unsigned Cols = 7;
// A helper function to print the seats
void print_seats(const int (&seats)[Rows][Cols]) {
for(auto& row : seats) {
for(int col : row) {
std::cout << col << "\t";
}
std::cout << '\n';
}
}
int main() {
int s[Rows][Cols] = {{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}};
std::cout << "Seats Available:\n";
print_seats(s); // Use the helper function
std::cout << "Enter Seat Number: ";
// Declare a separate variable for the input. It's an `unsigned int` here
// to only be allowed to hold positive integers (including 0).
unsigned seat;
if(std::cin >> seat) { // check that input succeeds
--seat; // make it 0-based(*)
if(seat >= Rows * Cols) { // check that it's within the allowed range
std::cout << "invalid seat\n";
} else { // the user entered a valid seat number
unsigned row = seat / Cols; // convert seat to row
unsigned col = seat % Cols; // convert seat to column
s[row][col] = 0; // set it to 0
print_seats(s); // print seats again
}
}
}
* - If the user enters 0, an unsigned like seat will wrap around when doing --seat to become a positive value. -1 will for example become the largest value an unsigned int can hold which will be much larger than Rows * Cols.
