2025-10-07 09:19:49 +02:00

66 lines
1.7 KiB
C

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
const char * PROMPT = "ml' nob:";
const char * INPUT_OKAY = "Qapla'";
const char * OUT_OF_RANGE = "Qih mi'";
const char * NOT_A_NUMBER = "Neh mi'";
const char * INVALID_NUMBER = "bIjatlh 'e' yImev";
const char * QUOTES[9] = {
"noH QapmeH wo' Qaw'lu'chugh yay chavbe'lu' 'ej wo' choqmeH may' DoHlu'chugh lujbe'lu'.",
"bortaS bIr jablu'DI' reH QaQqu' nay'.",
"Qu' buSHa'chugh SuvwI', batlhHa' vangchugh, qoj matlhHa'chugh, pagh ghaH SuvwI''e'.",
"bISeH'eghlaH'be'chugh latlh Dara'laH'be'.",
"qaStaHvIS wa' ram loS SaD Hugh SIjlaH qetbogh loD.",
"Suvlu'taHvIS yapbe' HoS neH.",
"Ha'DIbaH DaSop 'e' DaHechbe'chugh yIHoHQo'.",
"Heghlu'meH QaQ jajvam.",
"leghlaHchu'be'chugh mIn lo'laHbe' taj jej."
};
int main() {
char *buf = NULL;
size_t len = 0;
char* end;
printf("%s\n", PROMPT);
// Use getline for dynamic allocation
ssize_t chars_read = getline(&buf, &len, stdin);
errno = 0;
long input = strtol(buf, &end, 10);
// Check whether a range error occured or
// No characters were read by getline
// or check that strtol didnt move a single char
if(errno == ERANGE || chars_read == -1 || end == buf) {
printf("%s\n", NOT_A_NUMBER);
return EXIT_FAILURE;
}
// getline always adds the \n to the result string
// If the ending char by strtol is not that newline
// It means the string is not a whole number
// (e.g. 12abc, 1.23)
if(*end != '\n') {
printf("%s\n", INVALID_NUMBER);
return EXIT_FAILURE;
}
if(input < 0 || input > 8) {
printf("%s %ld\n", OUT_OF_RANGE, input);
return EXIT_FAILURE;
}
printf("%s\n", INPUT_OKAY);
printf("%s\n", QUOTES[input]);
free(buf);
return EXIT_SUCCESS;
}