0

Is there any way to do this?.

Lets take this code as an example:

int num;
printf("enter a number: ");
scanf("%d",&num);
printf("<- this is your number.");

Output will be like this:

enter a number: 2
<- this is your number.

What I want it to be:

enter a number: 2<-this is your number.
gsamaras
  • 71,951
  • 46
  • 188
  • 305
Eran
  • 81
  • 1
  • 6

4 Answers4

3

You can't do this with scanf. Depending on your platform(linux, windows, ...) you should use the library ncurses or similar.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
1

thank you guys for the help. after doing some searching find this:

int num;
printf("Enter a number: ");
scanf("%d", &num);

CONSOLE_SCREEN_BUFFER_INFO coninfo;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &coninfo);
coninfo.dwCursorPosition.Y -= 1;    
coninfo.dwCursorPosition.X += 20;    
SetConsoleCursorPosition(hConsole, coninfo.dwCursorPosition);

printf("<- this is your number.");

output: enter a number. 2 <- this is your number.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Eran
  • 81
  • 1
  • 6
0

I am assuming what you are seeing in your terminal when running the application is stdin being echoed.

What that means is that stdin is going to echo what it receives to stdout.

As Chux mentions above this is platform dependent (and terminal application dependent) behavior.

Good information here:

1.Hide password input on terminal 2. echoing of getchar and '\n' char from stdin

Community
  • 1
  • 1
bentank
  • 616
  • 3
  • 7
0

There is no portable way to do that, because by default input is line buffered and echoed at low level in current platforms (at least Windows and Unix-like systems).

There are ways not to use line buffered input or to use manual echo, but it will be platform dependent.

Under Linux for example, you could put input in non canonical mode (see man termios) : that would allow you to read data one char at a time (for example) with echo off, do the echo manually, do not echo the newline but process the input buffer (with sscanf) after a newline.

Under Windows, you could use the function getch (declared in conio.h) to also get one character at a time without echo. And I have no idea on how to do that on a Mac.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252