Displaying arrays and accepting inputs from the user

closed account (9G3v5Di1)
Write a C++ program with the following specifications:
1.Use two-dimensional array with size 7 columns and 5 rows.
2.Seat numbers are populated during run-time.
3.User is asked to input a seat number.
4.Seat number chosen is replaced by 0.
5.Program displays a remark “Seat successfully reserved” when reservation is done.
6.User is not allowed to reserve a previously reserved seat. Display “Seat is taken” remarks.
7.User is not allowed to enter an invalid seat number. Display an error message.
8.Program continuously loops.

Sample Output:
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 seat number to reserve : 11


1	2	3	4	5	6	7 
8	9	10	0	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 seat number to reserve :


This is what I've done so far:

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
  #include <string>
#include <iostream>
#include "_pause.h"

using namespace std;

int main() {
	int array[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}};

	for(int row = 0; row < 5; row++){
		for(int column = 0; column < 7; column++){
			cout << array[row][column] << "\t";
		}
		cout << endl;
	}

	cout << endl;

	int seatNumber;
	cout << "Enter a seat number to reserve: ";
	cin >> seatNumber;

	std::cin.get();
	_pause();
	return 0;
}


1) I've displayed the arrays
2) I've created a user input
3) I do not know if I'm correct with no.2 and what's next..
You did no. 2 on line 8 by defining the array.
Now you probably need some code to run through the array and find the value that's equal to your output, then replace the value by 0.
You need to check if the seat was previously taken (if value is already equal to 0)
You need to check if the input is correct ( 0 < input < 36 )
You probably need a while or do while loop to continually reserve seats until all seats are taken (if I understand your task properly)

Hope this helps
Last edited on
I would think that the next step is using the user input you have done to fine the seat in the array and marking it as reserved.
2.Seat numbers are populated during run-time.
I don't think that number 2 is correct. You populate the seats at compile time.

BTW. Do you have learned functions yet? They would make it a bit easier.
Also better is to use constants for the rows and column count

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
Hello goldwinbryanr,

After I posted my message I realized that subtracting one after user input is wrong. Please disregard what I did with line 35. This works great for a 1D array, but with a 2D array you need to get the seat number into a "row" and "column.

To expand on what hoogo said:
Now you probably need some code to run through the array and find the value that's equal to your output, then replace the value by 0.

I am thing a function with a nested for loop should do the trick.

Your code is coming along, but as hoogo says:
You probably need a while or do while loop to continually reserve seats until all seats are taken (if I understand your task properly)
Easy enough to add later when you have the code working.

Hope that helps,

Andy
closed account (9G3v5Di1)
learning functions comes after this one. So I guess I should not use a function. I appreciate the codes and understood it but not the line 56 std::cout << std::setw(6).. what is this? Okay so I think what you gave to me is the same as what I have done. I am having a hard time thinking on how will I run both arrays and inputs using nested loops..

and btw, what is the use of std:: ... ?
Last edited on
Hello goldwinbryanr,

The "setw()" comes from the "iomanip" header file. In simple terms it does what "\t" does. The difference would be that "setw()" uses an exact number of space for the output in the ()s where as "\t" could be different on different computer systems because the tab could be set up with a different amount of space. If you are not sure about it stick with the "\t".

"iomanip" also gives you the use of "std::fixed", "std::showpoint" and what is most often used "std::setprecision()". "std::fixed" tells "std::cout" to use a fixed point notation not scientific notation, i.e., you see 3.14159 not 3.14159e5. "std::showpoint" means that if the number would be 3.00 to show the .00. The "std::setprecision()" is how many places will show to the right of the decimal point.

There are several different namespaces in the C++ language. Standard or "std" is probably the largest namespace you will work with.

The "std::" is how you qualify something in the standard name space to use it. The line using namespace std; does this for you, but it is not selective at compile time the compiler tries put "std::" in front of everything. Some times this does not work and will give you an error message that is hard to understand.

It is better to learn what is in the standard namespace and how to qualify it than to take the laze way out just because some book writer did not want to take the time to qualify items in all their code or some instructor does not want to deal with seeing "std::" in a program. It is also better to learn what is in the standard name space now when it is a little at a time than all at once later.

I have seen many people think they are making their life easier by using using namespace std; only to find some day that their code will not compile and they can not figure out why.

I have been told this is not the best program, but it does prove a point:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <limits>

using namespace std;

int count{};

int main()
{
	count++;

	cout << count << endl;
	//std::cout << count << std::endl;

	// <--- 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();

	return 0;
}

Compile the program as is and see what happens. Then comment line 4 and switch the comment marks on 12 and 13 and see what happens when you compile the program. You do have to worry about the second header file or the code at the bottom just above return.

There are many posts and maybe some articles here to explain about using namespace std;. If you do a search on this site. Or a Google search works to.

This article is written about hearer files in that sense it does not really apply right now, but the concepts are worth understanding and the information about header files will be useful later.

http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

I hope the helps to explain things. If nor let me know and I will try again tomorrow when I am more awake.

Andy
closed account (LTbpSL3A)
Hi,

could please help me, how to solve this? i am a beginner for this and it is hard for me to solve this. i hope someone help me.

Thank you
closed account (9G3v5Di1)
let's continue this thread here http://www.cplusplus.com/forum/beginner/243394/
Topic archived. No new replies allowed.