Array is malfunctioning after do loop

I am trying to create an Airplane Seating reservation program. The whole program is working fine but after the do loop runs, the letter A from s does not appear where it should appear

ex: Seat 1A is available
Seat 1B is reserved
Seat 1C is available
Seat 1D is available

do loop runs

Seat 1 is available
Seat 1B is TAKEN
Seat 1C is available
Seat 1D is 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
36
37
38
39
40
41
42
#include<stdio.h>
#include<conio.h>
#include<iostream>
main()
{
     char s[4]={'A','B','C','D'},p,b;
    int a,c,x,y,seat[5][4];
    do
    {
    system("cls");
    printf("Please enter the seats to be reserved: ");
    scanf("%d%c", &a,&b);
    for(y=0;y<5;y++)
      {
                       printf("\n\n");
                       for(x=0;x<4;x++)
                       {
                                      if(y==a-1&&s[x]==b)
                                       {
                                       if(seat[y][x]==1)
                                       printf("\nSELECTED SEAT IS ALREADY TAKEN");
                                       else
                                       {
                                       seat[y][x]=1;
                                       c=1;
                                       printf("\nSeat %d%c is now reserved",y+1,s[x]);
                                       }
                                       }
                                       else if(seat[y][x]==1&&c==1)
                                       printf("\n\t\tSeat %d%c is TAKEN",y+1,s[x]);
                                       else
                                       printf("\nSeat %d%c is available",y+1,s[x]);
                       }
      }
      getch();
      printf("\n\nDo you want to try again? Y/N ");
      scanf("%s", &p);
      }
      while(p!='N');
    return 0;
}


PS: I am currently studying C programming; I have no idea how C++ works
Last edited on
1
2
3
char p;
//...
scanf("%s", &p);
See the problem?
Alxso, your seat array is uninitialized. i.e. contains garbage.

What is the purpose of variable c? A meaningful name would help alot here. And by extension, what is the difference between lines 21 and 31?
BTW, c is also uninitialized.
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
#include<stdio.h>
#include<conio.h>
#include<iostream>
main()
{
     char Column[4]={'A','B','C','D'};
     char repeat,SeatColumn;
    int SeatRow,checker,x,y,seat[5][4];
    do
    {
    system("cls");
    printf("Please enter the seats to be reserved: ");
    scanf("%d%c", &SeatRow,&SeatColumn);
    for(y=0;y<5;y++)
      {
                       printf("\n\n");
                       for(x=0;x<4;x++)
                       {
                                      if((y==SeatRow-1)&&(Column[x]==SeatColumn))
                                       {
                                       if(seat[y][x]==1)
                                       printf("\nSELECTED SEAT IS ALREADY TAKEN");
                                       else
                                       {
                                       seat[y][x]=1;
                                       checker=1;
                                       printf("\nSeat %d%c is now reserved",y+1,Column[x]);
                                       }
                                       }
                                       else if(seat[y][x]==1&&checker==1)
                                       printf("\n\t\tSeat %d%c is TAKEN",y+1,Column[x]);
                                       else
                                       printf("\nSeat %d%c is available",y+1,Column[x]);
                       }
      }
      getch();
      printf("\n\nDo you want to try again? Y/N ");
      scanf("%s", &repeat);
      }
      while(repeat!='N');
    return 0;
}


Note: x,y are row and column, respectively

scanf("%s", &repeat);
I used string instead of character because using the latter makes the program skip the part where it should get the value of repeat, hence leading to an infinite error loop.

Line 21 prints the message "Seat is already taken" if the seat entered was already selected by the user.

Line 30 checks the seats if the seat is available or taken, hence the use of checker. The idea is similar to light switches.

The array Seat[5][4] will pre-initialize the seats instead of the user inputting the seats one-by-one.
Last edited on
scanf("%s", &repeat); This is the core of your problem. You are trying to fit string in char. It will overwrite some random memory after this character. You can try to enter something like "aVeryLongString" instead of single character and see what happens.
Line 21 prints the message "Seat is already taken" if the seat entered was already selected by the user.

Line 30 checks the seats if the seat is available or taken, hence the use of checker.

You missed my point. Why do you do you have two different checks and messages if the seat is taken? Either the seat is taken, or its not.

The array Seat[5][4] will pre-initialize the seats instead of the user inputting the seats one-by-one.

No, it will not. Also checker is still uninitialized. Declaring local variables does NOT pre-initialize them.

Why do you have nested for loops? The if statement at line 19 defeats the purpose of the for loops. You might as well change the scanf to input x and y directly and eliminate SeatRow and SeatCol.

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
#include<stdio.h>
#include<conio.h>
#include<iostream>
main()
{
     char Column[4]={'A','B','C','D'};
     char repeat,SeatColumn;
    int SeatRow,checker,x,y,seat[5][4];
    do
    {
    system("cls");
    printf("Please enter the seats to be reserved: ");
    scanf("%d%c", &SeatRow,&SeatColumn);
    for(y=0;y<5;y++)
      {
                       printf("\n\n");
                       for(x=0;x<4;x++)
                       {
                                      if(seat[y][x]==1&&checker==1)
                                           printf("\n\t\tSeat %d%c is TAKEN",y+1,Column[x]);
                                      else if((y==SeatRow-1)&&(Column[x]==SeatColumn))
                                      {
                                           seat[y][x]=1;
                                           checker=1;
                                           printf("\nSeat %d%c is now reserved",y+1,Column[x]);
                                      } 
                                      else
                                           printf("\nSeat %d%c is available",y+1,Column[x]);
                       }
      }
      getch();
      printf("\n\nDo you want to try again? Y/N ");
      scanf("%c", &repeat);
      }
      while(repeat!='N');
    return checker;
}


Thank you MiiNiPaa and AbstractionAnon :)

Its now working A-OK.
I repeat my previous comments:
Golfer22 wrote:
The array Seat[5][4] will pre-initialize the seats instead of the user inputting the seats one-by-one.
AbstractionAnon wrote:

No, it will not. Also checker is still uninitialized. Declaring local variables does NOT pre-initialize them.

Why do you have nested for loops? The if statement at line 19 defeats the purpose of the for loops. You might as well change the scanf to input x and y directly and eliminate SeatRow and SeatCol.
Last edited on
Topic archived. No new replies allowed.