Hello goldwinbryanr,
You have a good start to your program.
I half agree with Thomas1965 "You populate the seats at compile time.", That comes from initializing the array, but at run time you change the array.
Hera are some hints and suggestions:
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
#include <iomanip>
#include <string>
//#include "_pause.h"
//using namespace std; // <--- Best not to use.
// Constants for the program
constexpr size_t MAXROW{ 5 };
constexpr size_t MAXCOL{ 7 };
// Proto Types
void DisplaySeats(int seats[MAXROW][MAXCOL]);
int main()
{
int seats[MAXROW][MAXCOL] =
{
{ 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 }
};
DisplaySeats(seats);
std::cout << std::endl;
int seatNumber;
std::cout << "Enter a seat number to reserve: ";
std::cin >> seatNumber;
seatNumber--;
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
//_pause();
return 0;
}
void DisplaySeats(int seats[MAXROW][MAXCOL])
{
for (int row = 0; row < 5; row++)
{
for (int column = 0; column < 7; column++)
{
std::cout << std::setw(6) << seats[row][column];
}
std::cout << std::endl;
}
}
| |
You did not include the file or function "_pause.h" with your post. So I am not sure what is there. The last four lines above the "return" statement are what I use for a pause. Feel free to use them if you like.
Line 7. The comment explains it.
Lines 10 and 11 are a better way to define an array. This way you only have one place to make a change if it is needed. As a global variable, the only king I will suggest, the whole program has access to them. An example of what Thomas1965 is saying.
Line 18. Because the compiler does not care about white space or blank lines I have found that when working with a 2D array this is a better visual understanding of what you are working with because it looks like rows and columns. Also I would not use the name "array" as there is a "std::array" form the array header file and even though the header file is not used it could be a potential problem. The name 'seats" better describes what the array is for.
Line 27. This function call can be used any time in "main" to display the "seats" array when changes are made.
Line 31. is OK here, but my personal preference is to put all variables at the beginning of the function so I know where to find them. To bury a variable in a program sometimes makes it hard to find.
The next line input a seat number and are not a problem.
Do remember that an array starts with element zero, so your 35 element array will go from zero to 34 for a total of 35 elements. After you input a seat number you need to subtract one to access the correct element of the array.
Since I did not have the "_pause.h" code this is what I use with my VS. Should do the same as your "_pause.h" and it can be put in a function if needed. I mostly use it in debug mode and comment it out if I compile the program for release. Personally I do not use the "_" to start a variable name, I tend to leave that to header files, but I have used it in the middle of a variable name form time to time.
Now that you can display the array and get user input for the seat it is time to work on the code to change the array.
Forgot to mention in the code you see "size_t", or sometimes I use "std::size_t", you can very simply think of this as another name for "unsigned int" and since it is used for defining an array the size can only be a positive number. The "size_t" is just a way to help you keep a positive number.
Hope that helps,
Andy