0

When I'm using getch() in my program, I get this odd character that appears to be sent to stdin. This character is (B. Here's my code:

#include "structures.h"
#include "functions.h"

char choice;
int option;
char *ptr;

int main(void) {
    while(1) {
        puts("============================================Select an Option============================================");
        puts("1. Create Linked List");
        puts("2. Display items");
        puts("3. Add item to the beginning");
        puts("4. Add item to the end");
        puts("5. Add item in the middle");
        puts("6. Delete item from the beginning");
        puts("7. Delete item from the end");
        puts("8. Delete item from the middle");
        puts("9. Exit");
        printf(">>> ");

        initscr();
        choice = getch();
        option = choice - '0'

        switch (option) {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
            case 8:
                break;
            case 9:
                puts("\033[0;32mExiting...\033[0m");
                return 0;
            default:
                handle_error("Invalid option. Please enter a number between 1-9");
                continue;
        }
        break;
    }
    endwin();
    return 0;
}

I'm not going to include the header files I created since they don't have anything to do with this. Does anyone know how to fix this?

Matthew Schell
  • 599
  • 1
  • 6
  • 19
  • I guess this is the `getch()` function from (n)curses? It's not standard C, so additional tags would be good. – Nate Eldredge Apr 14 '21 at 14:36
  • I bet that it has something to do with `initscr()` which is an ncurses call (I think). At anyrate print the char as hex so that we can tell what the char really is. printf("%02X\n",choice); – shrewmouse Apr 14 '21 at 14:37
  • 1
    You can't pass a single char to strtol. It expects a null-terminated string. – stark Apr 14 '21 at 14:38
  • 1
    You need to study strings before using them. A single `char` is not a string, see the linked duplicate. C doesn't have a string class like most other languages do. So your call `strtol(&choice...` corrupts the variable values and will likely cause a crash etc. – Lundin Apr 14 '21 at 14:56
  • @shrewmouse I pasted that line of code in and after just pressing enter with the (B character already inputted for me, it says `0A` – Matthew Schell Apr 14 '21 at 17:09
  • @stark My problem isn't the strtol, its the getch() function. Also, see my edit for that line – Matthew Schell Apr 14 '21 at 17:11
  • `0A` is the hex code for `\n`, which is the "enter" you typed. You cannot avoid it. So you need to take some measures. – the busybee Apr 14 '21 at 19:49
  • @thebusybee i didn't even press enter. When getch() allows me to input something you see i have `>>> ` before it to signify user input. I didn't put `>>> (B`. the extra character was somehow added by getch(). – Matthew Schell Apr 14 '21 at 19:55
  • Well, `(B` is not a character, there are two of them (at least). Please show us the hex values of the result that `getch()` gives you. Is it possible that you typed a cursor key or so? – the busybee Apr 14 '21 at 20:02
  • No I didn't touch my keyboard when i executed the program. The character just appears there, right after the `>>> `. And isn't 0A the hex value of that? i'm not sure i understand what you're asking – Matthew Schell Apr 14 '21 at 20:05
  • So you are saying, you start your program, and without any additional key pressed the function `getch()` immediately returns `0A`? O-o, I never saw this. (I'm sorry, at my place it's time to go to sleep... I'll come back tomorrow.) -- In the meantime, what does the documentation of `getch()` say, does it return even if no key was pressed? If so, what is the return value? Or does it block until a key is pressed? – the busybee Apr 14 '21 at 20:20
  • According to [this stack overflow question](https://stackoverflow.com/questions/25045096/what-does-getch-in-turbo-c-return-i-am-sure-they-are-not-ascii-values-for-key#:~:text=getch%20()%20function%20returns%20two,the%20key%20that%20was%20pressed), One person says that it returns an `int` while another says that it returns 2 keycodes for arrow keys. There are more details on the post. – Matthew Schell Apr 16 '21 at 13:33

0 Answers0