From 8b0dbfb1209291ecf246cd2582c3ce8197ad7894 Mon Sep 17 00:00:00 2001
From: Yurii Chechur <yurii.chechur@student.tuke.sk>
Date: Tue, 24 Dec 2024 17:21:43 +0000
Subject: [PATCH] Update sk1/main.c

---
 sk1/main.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/sk1/main.c b/sk1/main.c
index 2e3cbea..fdbdb09 100644
--- a/sk1/main.c
+++ b/sk1/main.c
@@ -26,8 +26,11 @@ int main() {
         // Запитуємо користувача про вибір
         printf("Enter your choice (0 to exit): ");
         if (scanf("%d", &choice) != 1) {
-            fprintf(stderr, "Invalid input. Exiting.\n");
-            break;
+            // Якщо введення не є числом, виводимо повідомлення про помилку
+            fprintf(stderr, "Invalid input. Please enter a valid number (0-4).\n");
+            // Очищаємо буфер введення від непотрібних символів
+            while(getchar() != '\n');
+            continue;
         }
 
         if (choice == 0) {
@@ -35,12 +38,34 @@ int main() {
             break;
         }
 
+        if (choice < 0 || choice > 4) {
+            // Перевіряємо, чи вибір знаходиться в допустимому діапазоні
+            printf("Invalid choice. Please select a valid option (1-4).\n");
+            continue;
+        }
+
         // Вводимо імена файлів
         printf("Enter input file name: ");
         scanf("%s", infile);
         printf("Enter output file name: ");
         scanf("%s", outfile);
 
+        // Перевіряємо, чи існує вхідний файл, і чи доступний вихідний файл для запису
+        FILE *infile_ptr = fopen(infile, "rb");
+        if (!infile_ptr) {
+            printf("Error: Input file does not exist or cannot be opened.\n");
+            continue;
+        }
+        fclose(infile_ptr);
+
+        FILE *outfile_ptr = fopen(outfile, "wb");
+        if (!outfile_ptr) {
+            printf("Error: Output file cannot be created or opened.\n");
+            continue;
+        }
+        fclose(outfile_ptr);
+
+        // Виконання обраного алгоритму
         switch (choice) {
             case 1:
                 compress_1(infile, outfile);