Printing to same line.

Subscribe to Printing to same line. 5 posts, 2 voices

 
Avatar jeongkyutaeng 2 posts

I have to use enter key for printing a result to a terminal.

example> input : 2 3 result : 5

The example’s scenario is the following. First, Inputing 2 and 3 Second, Clicking enter key Finally, 5 is printed.

This is so simple. By the way, I have to print the result to same line like the following.

- input : 2 3 result : 5

Plz help me.

ps : I am using C and working in linux

 
Avatar Kyle 6 posts

This should work:

include <conio>

int main() { int x,y; char space, break; printf(”- input : “); x = (int)(_getche() – 48); space = _getche(); if (space != ’ ‘) { printf(”\r\nExpecting space after first summand.\r\n”); return 0; } }

y = (int)(_getche() - 48);
break = _getch();
if(break == '\n') {
  printf(" result: %d", x + y);
} else
  printf("\r\nExpecting carriage return after 2nd summand.\r\n");
}
return 0;

Sorry for the lack for formatting. This is mailer code. IT might not work exactly.

 
Avatar Kyle 6 posts

OK. I have no idea why it formatted it like that. Are there code flags you can put around code so it formats it properly?

 
Avatar jeongkyutaeng 2 posts

Thank you. I got a hint,it is getch() function, to you and I made it.

 
Avatar Kyle 6 posts

Yep they’re in conio.h

use _getch() though, not getch(), for better portability.

Also, _getche() grabs the char and echoes it to screen as well, where as _getch() just grabs it with no echo.

Glad I could help even though I couldn’t format my code snippet for you!