Hello, I need to do a string input function, the string is entered until the user presses ENTER. Here is the actual code itself:
char* GetLine() { char* line = NULL; char ch; int i = 0; while(true){ ch = getch(); //Считываем, но не выводим символ switch(ch) { case '\r': line = (char*)realloc(line, (i + 1) * sizeof(char)); line[i++] = '\0'; //Признак конца строки return line; case '\b': printf("\b \b"); //Удаляем символ в консоле if (i > 0) i--; break; default: putchar(ch); line = (char*)realloc(line, (i + 1) * sizeof(char)); line[i++] = ch; } } } What is the actual problem, I need that when I press the individual keys (F1-F12, Esc, numbers on the numpade, etc.) they are not displayed in the console. I know that you can use a switch to scan all keys with a scancode, but it seems to me that the solution should be easier.
getchreturns 0, ignore the next return. With memory allocation, your approach is incorrect - to allocate for each character. Select a piece, if necessary - double, it will be much more efficient. And the last - the task is worth it - exactlygetch()? Do not just read, dynamically increasing the size of the buffer, namely this function? because all the same is done much easier throughgetcharfor example, and without these perversions ... - Harry