1

image

I am working on a seat reservation program and here are some of its parts. My objective is to mark 'X' on the array using the user input. This is based on a tic tac toe program but I need to mark X only in the table. I am asking if I am doing the loop for the program correctly and if not what I use to fix it. I am facing errors but I don't know if this is the right loop for them.

I want to mark X based on the input of rows and columns that they are given and if it is already entered or marked. I want to say that it is already taken and will prompt the user again if they want to enter a new input again and if not they will be returned to the main menu again.

typedef struct{
    char city[20], name[50], seatcol;
    int age, seatrow, id;

}passenger;

char seat[ROWS][COLS];

void reserve(){

passenger p;
do {
    printf("\n\t\t\tEnter your seat number:");
    printf("\n\t\t\tROW:(1-10)  ");
    scanf("   %d",&p.seatrow);
    printf("\t\t\tCOLUMN:(A-F)  ");
    scanf("   %s ", p.seatcol);

    if(p.seatrow == 1 && p.seatcol == 'A')(
        seat[0][0]= 'X');
    else if(p.seatrow == 1 && p.seatcol == 'B')(
        seat[0][1]= 'X');
    else if(p.seatrow == 1 && p.seatcol == 'C')(
        seat[0][2]= 'X');
    else if(p.seatrow == 1 && p.seatcol == 'D')(
        seat[0][3]= 'X');
    else if(p.seatrow == 1 && p.seatcol == 'E')(
        seat[0][4]= 'X');
    else if(p.seatrow == 1 && p.seatcol == 'F')(
        seat[0][5]= 'X');
    //2
    else if(p.seatrow == 2 && p.seatcol == 'A')(
        seat[1][0]= 'X');
    else if(p.seatrow== 2 && p.seatcol == 'B')(
        seat[1][1]= 'X');
    else if(p.seatrow == 2 && p.seatcol == 'C')(
        seat[1][2]= 'X');
    else if(p.seatrow == 2 && p.seatcol == 'D')(
        seat[1][3]= 'X');
    else if(p.seatrow == 2 && p.seatcol == 'E')(
        seat[1][4]= 'X');
    else if(p.seatrow == 2 && p.seatcol == 'F')(
        seat[1][5]= 'X');
    else{
        printf("Invalid option!");
        p.id--;
        getch();
    } 
    p.id++;
    status = (p.seatrow && p.seatcol != seat);
    }while(status);
    if(!status){
        printf("\n\t\t Already allocate seat. Choose another seat? (Y/N)");
        scanf("%s", answer);

        if(answer == ' Y'){
           printf("\n\t\t\tROW:(1-10)  ");
           scanf("   %d",&p.seatrow);
           printf("\t\t\tCOLUMN:(A-F)  ");
           scanf("   %s ", p.seatcol);
        }
        else{
            printf("Your data will be not saved and will be returned to main menu:");
            mainmenu();
        }
    }
}
gwen
  • 41
  • 5
  • Please give the exact test input, expected result and actual result. – kaylum Jul 18 '22 at 06:13
  • `scanf(" %s ", p.seatcol);` That is wrong for a few reasons. Firstly `seatcol` is a `char` and not a pointer as required for `scanf`. Secondly, `%s` is for a string and a single char cannot store anything except the empty string. Need to use `%c` or use an array instead of a single char. – kaylum Jul 18 '22 at 06:15
  • Possibly off-topic. `" %d"` and `" %s "` Why are you putting spaces before and after the format specifier. Trailing whitespace in particular is a common source of problems: [What is the effect of trailing white space in a scanf() format string?](https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string) – kaylum Jul 18 '22 at 06:19
  • @kaylum Thank you, I'll take note of that. I also inserted an image of my program output for you to see but full of errors yet so it stopped there. I need to insert 'X' on the table to mark if it is already taken by other users I need to ask them to change seats and if not, I will direct them to the menu again. – gwen Jul 18 '22 at 06:29

0 Answers0