Skip to content

Program: Simple House Room Allocation in C #24

Description

@rolandmezatsa-sys

#include <stdio.h>
#include <string.h>

#define MAX_CHAMBRES 10

typedef struct {
int numero;
char locataire[50];
int occupee; // 0 = free, 1 = occupied
} Chambre;

void afficherChambres(Chambre chambres[], int n) {
printf("\n--- Room Status ---\n");
for (int i = 0; i < n; i++) {
printf("Room %d : %s\n", chambres[i].numero,
chambres[i].occupee ? chambres[i].locataire : "Free");
}
}

void allouerChambre(Chambre chambres[], int n) {
int num;
char nom[50];
printf("Room number to allocate (1-%d): ", n);
scanf("%d", &num);
if (num < 1 || num > n || chambres[num - 1].occupee) {
printf("Invalid room or already occupied.\n");
return;
}
printf("Tenant's name: ");
scanf(" %[^\n]", nom);
chambres[num - 1].occupee = 1;
strcpy(chambres[num - 1].locataire, nom);
printf("Room successfully allocated.\n");
}

void libererChambre(Chambre chambres[], int n) {
int num;
printf("Room number to free (1-%d): ", n);
scanf("%d", &num);
if (num < 1 || num > n || !chambres[num - 1].occupee) {
printf("Invalid room or already free.\n");
return;
}
chambres[num - 1].occupee = 0;
strcpy(chambres[num - 1].locataire, "");
printf("Room successfully freed.\n");
}

int main() {
Chambre chambres[MAX_CHAMBRES];
int choix;
for (int i = 0; i < MAX_CHAMBRES; i++) {
chambres[i].numero = i + 1;
chambres[i].occupee = 0;
strcpy(chambres[i].locataire, "");
}

do {
    printf("\n1. Show rooms\n2. Allocate room\n3. Free room\n0. Exit\nYour choice: ");
    scanf("%d", &choix);
    switch (choix) {
        case 1: afficherChambres(chambres, MAX_CHAMBRES); break;
        case 2: allouerChambre(chambres, MAX_CHAMBRES); break;
        case 3: libererChambre(chambres, MAX_CHAMBRES); break;
        case 0: printf("Program ended.\n"); break;
        default: printf("Invalid choice.\n");
    }
} while (choix != 0);

return 0;

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions