1

I have a little problem with one of my assignments. It's about a problem that goes as follows:

Using the Switch statement, write a program that reads values until the ENTER key is pressed. If the characters "Newline", "Tab", or "Backspace" are pressed, output their names.

Here's what I did so far.

int main(){
    char x;
    x=getchar();
    while(x!='\r')
    {
        switch (x)
        {
        case '\t':
            printf("Tab!");
            break;
        case '\n':
            printf("Newline!");
            break;
        case '\b':
            printf("Backspace!");
            break;
            default: break;
        }
            x = getchar();
    }
    /* switch(x){
        case '\r': break;
        case '\t': printf("Tab!");
        case '\n': printf("Newline!");
        case '\b': printf("Backspace!");
        default: x=getch();
    } */

    return 0;
}

My problem is that every time I read a character using getch() or scanf() I press enter, so... the program doesn't do much. Do you have any ideas on how I could resolve this problem? Also isn't Newline same key as Enter?

I am using MinGW64 in Windows 10 and VScode as the IDE.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
Alex Fulop
  • 23
  • 4
  • First: newline is not a key, it is a character! It is possible to read without pressing enter in many environments but it is *platform specific*, yet you do not say whether this is on Windows, Unix, embedded... [`getchar` returns an *int*.](https://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-fgetc-and-putchar-fputc) – Antti Haapala -- Слава Україні Nov 17 '19 at 17:39
  • Oh sorry about that, It is on Windows, how could I do it then? – Alex Fulop Nov 17 '19 at 18:11
  • Please edit your question to include that the. In any case, does this work?https://stackoverflow.com/questions/27653016/read-in-command-prompt-input-without-pressing-enter this? – Antti Haapala -- Слава Україні Nov 17 '19 at 18:15
  • With standard C and line buffered input, code gets nothing until `'\n'` (Enter) is entered. – chux - Reinstate Monica Nov 17 '19 at 18:20
  • The issue is not so much an OS one (Windows, Unix, embedded), but a compiler one. What compiler are you using? – chux - Reinstate Monica Nov 17 '19 at 18:24
  • 1
    I am Using MinGW64 and VScode as the IDE – Alex Fulop Nov 17 '19 at 18:28
  • That still may be a challenge. You need to use `tcsetattr` to put the keyboard in non-cannonical mode. This works fine on Linux, but I have not tried MinGW on windows. Windows has the ancient DOS `getch` (don't use it) that is similar. I'll have to check on `tcsetattr` in MinGW. – David C. Rankin Nov 17 '19 at 19:03
  • See [Where to obtain termios.h](https://stackoverflow.com/questions/20772965/where-to-obtain-termios-h) and [Mingw-notify mingw-Bugs-659559 vfs.h and statvfs.h not found](https://sourceforge.net/p/mingw/mailman/message/7280588/). MinGW only supports ANSI C not POSIX, so you will have to find a workaround (and heaven forbid `conio.h`). Just tested -- sigh... You can include `conio.h` with MinGW and get your non-cannonical mode behavior `:(` – David C. Rankin Nov 17 '19 at 19:30
  • Also, note, after you change `getchar()` to `getch()` you will not be able to match `'\n'` because the DOS line endings of `"\r\n"` will interpret the `'\r'` exiting the loop before the `'\n'` is reached. – David C. Rankin Nov 17 '19 at 22:02

1 Answers1

0

It's not clear from the requirement that the program should response after taking one character. May be no additional work is needed, as the output is the same, regardless of the line buffering of the input.

On Linux, you can move the terminal into 'raw' mode (either using system calls, or using stty). No such utility for Windows. If you MUST, look at: Get key press in windows console

Given this is basic Unix excercize, I doubt you need to implement the above.

dash-o
  • 13,723
  • 1
  • 10
  • 37