How to check if the seat is free and if you are not asking the user to type again by inserting an 'X' in the occupied space?

Asked

Viewed 43 times

0

I need the user to enter where he wants to sit by informing the row and column he wants, if the user chooses the position 0-1 the program will register and write to a txt file later mind that information, and will enter a looping to register a new user, where he will type what position he wants and if he type a position where it is already occupied by another user the program will not accept putting a X in place to demonstrate that it is occupied.

Follows the code...

#include<stdio.h>
#include <stdlib.h>

void main()
{
    printf("\n \nEscolha seu assento: \n\n");

    int m, n, fileira, assento;
    int lugares[10][16];

    for(m=1;m<10;m++){
        for(n=1;n<16;n++){
            printf("[%d- %d]", m, n);
        }

        printf("\n");   
    }

    printf("\nFileira desejada: ");
    scanf("%d",&fileira);
    printf("Assento desejado: ");
    scanf("%d",&assento);

    system("pause");
}
  • Since you use values 0 and 1, it makes no sense to use an integer. You could use an bool being true busy and false no. The loop should start from 0 also instead of 1 as it is doing.

1 answer

0


I guess it’s almost over by then. Just add this code already done in a while and create a boolean control variable to terminate the loop. I don’t know if I understand how you’ll manage the seats, but I’ll assume it’s 0 for vacant and 1 for busy. In your case then add:

lugares[m][n] = 0;

Then make the loop:

bool encerrar = false;

while (!encerrar)
{
    //seu código sem as declarações das váriaveis.

    if (lugares[fileira][assento] == 1)
        printf("Posicao Ocupada!\n");
    else
        lugares[fileira][assento] = 1;
}

I think there’s a good start to moving on..

  • bool understands the use of stdbool.h. It seems important to me that you mention this in your answer.

  • I did not understand the code right in itself, would you put the part I developed along with inclusion already? I’m sorry I’m kind of layman still in this area of programming.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.