0

I am very beginner in C and would like to code something which gets name from user and rearrange it with backspace, cursor(by using arrow keys).

In the program, when user writes his name and press ESC it has to stop the program and print the name on the screen.

So far, I was able to do ESC and Backspace parts with usign getch() and their ASCII codes, but I'm having problem on moving cursor by using arrow keys when user tries to rewrite or rearrange his name.

Here is my code:

#include<stdio.h>
#include<conio.h>

int main() {
  int i, k = 0;
  char character;
  char samplearray[100];

  printf("Enter your name:\n");

  while (1) {
    character=getch();

    if (character != 0x1B && character != 0x8 && character != 0x4B) {
      samplearray[k] = character;
      k++;
      printf("%c", character);
    }

    else if (character == 0x8) {        //Deleting procces in name and it works without any problem.
      samplearray[k];
      k--;
      system("cls");
      printf("Enter your name:\n");
      for (i = 0; i <= k; i++) {
        printf("%c", samplearray[i - 1]);
      }
    }

    else if (character == 0x4B) {        //Having a problem on this part.
      samplearray[k] = character;
      k--;
      printf("\b\b", samplearray[k]);
    }

  else
    break;
  }
  printf("\nYour name is: \n");
  for (i = 0; i <= k - 1; i++) {
    printf("%c", samplearray[i]);
  }
  getch();
  return 0;
}
the busybee
  • 10,755
  • 3
  • 13
  • 30
fenderogi
  • 118
  • 7
  • Which target system are you using? For example, the common "cmd" on Windows is too primitive and has very few control commands. -- The line `printf("%c", samplearray[i - 1]);` accesses `samplearray[-1]` out-of-bound. -- The value 0x4B is the character 'K', did you mean that for deleting backwards? – the busybee Jan 07 '21 at 10:41
  • I am using "cmd" on Windows. About the 0x4B, when i checked the ASCII codes of Left Arrow Key in google i found(75 or 0x4B) and tried to use it. On that part of the code i would like to go back in the name input that user puts with the cursor one or more steps. – fenderogi Jan 07 '21 at 10:45
  • OK. The 'K' is the second of a two-character sequence that the input driver generates. You will need to catch the first character, too. If not, your user cannot input 'K'. -- I would limit the editing capabilites to the bare minimum: delete single characters backwards with the "backspace" key, and "enter" to finish the input. – the busybee Jan 07 '21 at 10:53
  • The function and cursor control keys return *more than one* value (so call it again when the first is detected). You can easily find them for yourself with a simple test loop using `while(1) printf("%d\n", getch());`, also please see [What are the ascii values of up down left right?](https://stackoverflow.com/questions/2876275/what-are-the-ascii-values-of-up-down-left-right) For function keys, it's a different value for the first. – Weather Vane Jan 07 '21 at 10:56
  • @WeatherVane When i tested the Left Arrow Key it returns me 2 values as you said, 75 and 224. But i still have problem to write those part in the code. – fenderogi Jan 07 '21 at 11:36
  • @thebusybee You are right, i didn't test the "K" before your comment but now i can't input "K". – fenderogi Jan 07 '21 at 11:39
  • That is because it produces `224 75` not `75 224`. I suggest you use a `switch(keypress)` statement with `case 224:` calling `getch()` again and another `switch` statement inside it. – Weather Vane Jan 07 '21 at 11:41
  • Right, 224 is 0xE0, which is the prefix byte for special characters. -- Anyway, your program cannot use these sequences **in the output**, because the console of the standard "cmd" does not understand them. – the busybee Jan 07 '21 at 13:44
  • @thebusybee I see, is there any advices you can give to me? – fenderogi Jan 07 '21 at 13:45
  • As I suggested, implement just the minimum. Otherwise you need to "emulate" the required behavior with the primitive possibilities of cmd's console. It mainly understands just `'\a'` (alarm = beep), `'\b'` (non-destructive backspace), `'\t'` (tabulator fixed on multiples of 8), `'\r'` (carriage return = cursor fully to the left), and `'\n'` (new line = cursor one line down). -- You might consider to call Win32 API functions, which will give you a lot more of possibilities. Or you might consider to use a library like "ncurses". But recommendations are off-topic here on StackOverflow. – the busybee Jan 07 '21 at 13:52
  • Oh, be aware that `'\n'` in "text mode" will be replaced by `'\r'` + `'\n'`. – the busybee Jan 07 '21 at 13:53

0 Answers0