Seat Reservation for more than 1 (C programming)

Hi, can anyone help me with my coding? I am a beginner and I want to practice a lot but I have a limitation of knowledge about c programming. My coding must consist of
1. a seat available, if user choose unavailable seat(the one with x mark), it will display "seat unavailable, Please choose another seat"
2. when a user enters 5 persons, then they can choose 5 seats(this one I do not know how to do it)
3. display what seat they choose.

You may change or add new codes.

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
#include<stdio.h>

void Seat();

int main()
{
	int col, p, i, k;
	char seat;
	printf("==================================================\n");
	printf("|****************SEAT RESERVATION****************|\n");
	printf("==================================================\n\n");
	printf("How many person? \n");
	scanf("%f", &p);

	Seat();

	p = k;

	for (i = 1; i <= k; i++)
	{
		printf("Please Enter Number Seat %d (EXAMPLE: 6F): ", i);
		scanf(" %c", &seat);
	}

	return 0;
}

void Seat()
{
	printf("N0\tA\tB\tC\tD\tE\tF\tG\n");
	printf("1 \t*\t*\t*\t*\t*\t*\t*\n");
	printf("2 \t*\t*\t*\t*\tX\t*\t*\n");
	printf("3 \t*\t*\tX\tX\t*\t*\t*\n");
	printf("4 \t*\t*\t*\tX\tX\t*\t*\n");
	printf("5 \t*\t*\t*\t*\t*\t*\t*\n");
	printf("6 \t*\t*\t*\t*\t*\t*\t*\n");
	printf("7 \t*\t*\t*\t*\t*\t*\t*\n");
}
Last edited on
Hello adrazl,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button, but you may still to indent your code.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



have a limitation of knowledge about c++ programming.

That is apparent since you have a C program not C++.

So the first question is should this be a C program or a C++ program?

Before anyone spends any time here it would be nice to know which direction to go.

This is your code with a couple of blank lines to make it more readable:
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
#include<stdio.h>

void Seat();

int main()
{
	int col, p, i, k;
	char seat;
	printf("==================================================\n");
	printf("|****************SEAT RESERVATION****************|\n");
	printf("==================================================\n\n");
	printf("How many person? \n");
	scanf("%f", &p);

	Seat();

	p = k;

	for (i = 1; i <= k; i++)
	{
		printf("Please Enter Number Seat %d (EXAMPLE: 6F): ", i);
		scanf(" %c", &seat);
	}

	return 0;
}

void Seat()
{
	printf("N0\tA\tB\tC\tD\tE\tF\tG\n");
	printf("1 \t*\t*\t*\t*\t*\t*\t*\n");
	printf("2 \t*\t*\t*\t*\tX\t*\t*\n");
	printf("3 \t*\t*\tX\tX\t*\t*\t*\n");
	printf("4 \t*\t*\t*\tX\tX\t*\t*\n");
	printf("5 \t*\t*\t*\t*\t*\t*\t*\n");
	printf("6 \t*\t*\t*\t*\t*\t*\t*\n");
	printf("7 \t*\t*\t*\t*\t*\t*\t*\n");
}


Andy

Edit: Added the code.
Last edited on
Hi Andy,

I’d like to express my deepest appreciation for your guidance. You not only realized my mistake, but you also turned every mistake into a learning opportunity. Your guidance has been influential, and I know it will shape my developing skills. I'm sorry for the mistakes I made :) and thank you again for your reply.
You haven't answered Handy Andy's question about whether this is supposed to be a C or a C++ program.

Lines 13: You input p. Line 17: You overwrite p with the uninitialized values of k.

Line 17: You probably don't want to use a floating point format descriptor to input an integer. n It's not likely you're going to have a fractional number of passengers.

Line 19: You loop an indeterminate number of times. Your upper loop limit (k) is still uninitialized.

Line 21: You're giving the example of 6F, which is a row an column, but you scanf statement at line 22 is accepting only a single character.

Lines 30-37: You're printing a fixed seat map. You want to keep the seat map in a 2D array so that when you make a change, that change is reflected in the array and print out that array.

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
#include <stdio.h>
#include <memory.h>

#define NUM_ROWS 7
#define NUM_COLS 7

void Display_Seats(char seat_map[NUM_ROWS][NUM_COLS]);

int main()
{	int row, col, persons, i, k;
	char seat;
	char seat_map[NUM_ROWS][NUM_COLS];

	/*	Initialize the seat map */
	for (int i = 0; i < NUM_ROWS; i++)
		memset(seat_map[r], ' ', NUM_COLS);
	printf ("==================================================\n");
	printf("|****************SEAT RESERVATION****************|\n");
	printf("==================================================\n\n");
	printf("How many person? \n");
	scanf("%d", &persons);

	Display_Seats(seat_map);

	for (i = 1; i <= persons; i++)
	{	printf("Please enter row and column of seat %d (EXAMPLE: 6 F): ", i);
		scanf("%d %c", &row, &seat);
		col = seat - 'A';	/* Convert from char to int */
		if (seat_map[row][col] == 'X')
			printf("That seat has already been sold\n");
		else
			seat_map[row][col] = 'X';
	}
	Display_Seats(seat_map);		/* Display the updated seat map */
	return 0;
}

void Display_Seats(char seat_map[NUM_ROWS][])
{	printf("N0\tA\tB\tC\tD\tE\tF\tG\n");
	for (int r=1; r<=NUM_ROWS; r++)
	{	printf("%d ", r);
		for (int c = 0; c<NUM_COLS; c++)
			printf("\t%cd", seat_map[r][c]);
		printf("\n");
	}	
}
Last edited on
Hello adrazl,

You are welcome.

The question I have been waiting on an answer for is. Should this be a C program or a C++ program?. I do not know which direction to go with the code.

This does not matter whether it is a C or C++ program the use of "\t" can be a problem. Given a line in the "Seat" function printf("1 \t*\t*\t*\t*\t*\t*\t*\n"); and saying that the output is different,the (1) being something with more characters, the following "\t" may not create the proper spacing and could make the rest of the line not line up in the same place as the rest of the output.

Sometimes it is easier to just use a space over using a "\t". C++ has a function that can be used to space out what you want should you need a C++ program.

In a program one thing I have see most often is that a 2D "char" array is used to keep track of which seats are empty and which ones are taken.

Andy
Topic archived. No new replies allowed.