So I wrote the following simple program which asks the user for some input and afterwards just asks if they want to do that again or exit.
#include <conio.h> /* or use ncurses for getch() */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
void ReadConsoleInput(void) {
char buffer[83];
char *result;
printf("\nInput line of text, followed by carriage return:\n");
result = fgets(buffer, sizeof buffer, stdin);
buffer[strcspn(result, "\r\n")] = '\0';
if (!result) {
printf(
"An error occurred reading from the console:"
" error code %d\n",
errno);
} else {
printf("\nLine length = %d\nText = %s\n", (int)strlen(result), result);
}
}
int Repeat() {
printf("Again? (Y/N): \n");
int ch;
ch = getch();
return (ch == 'Y' || ch == 'y') ? TRUE : FALSE;
}
int main() {
do { /* infinite loop */
ReadConsoleInput();
} while (Repeat());
return 0;
}
The program runs fine under Windows using mingw64 compile, but the behavior changes upon changing conio.h to ncurses.h and compiling it under linux. The program runs the ReadConsoleInput()
function fine but after the user hits 'ENTER' the Repeat()
function just displays and the program ends before the user can enter any char.
What is happening different under linux?
(Also as an aside, if the infinite loop is not the right technique for what I am trying to do please advise. I feel like most programs don't rely on an infinite loop test to stay open.)
Eventually I plan on expanding the program to storing the text using a tree data structure and learn from there but I would like to maintain cross platform unity as I go.