1

I want to get input from special position of console. I called my gotoxy function to go to the specific position, then called gets or scanf to get input from that position.But the cursor waits for user input and do not input previous text the user has input. What is wrong with my code? This is my code:

//suppose we are in position (0,0)
printf("%s","Hello world!\n");
//now we are in position (0,1)
gotoxy(0,0);
scanf("%s",string);//or gets(string)

Now string should be "Hello world!" but it waits for user input.

My gotoxy:

void gotoxy(int x , int y){
COORD newPosition={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),newPosition);
}
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Mohammad
  • 362
  • 1
  • 7
  • 19

1 Answers1

3

gotoxy() will change the position of the cursor for future output operations, but this does not mean you can read from the screen.

scanf() is a standard function which reads from stdin, e.g. the keyboard. There is no way to use it to read characters previously printed on the screen. To do that, it would be much easier for you to simply maintain your own buffer of what characters you've written to the screen, and read from that buffer when needed.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I am a beginner,so ask this question:what is buffer? – Mohammad Jan 02 '15 at 08:32
  • 1
    A buffer is usually an array of something in memory, in this case a 2D representation of the screen that you can write characters into to retrieve them later. – John Zwinck Jan 02 '15 at 08:34
  • another question:Is there a simple way so that when console changes,our buffer changes contemporaneously? – Mohammad Jan 02 '15 at 08:46
  • I'm not sure. On *nix you could use ncurses and this answer: http://stackoverflow.com/a/3089190/4323 but since you're on Windows I don't know what the equivalent is. – John Zwinck Jan 02 '15 at 08:58