1

I'm trying to make a text adventure game, based on console environment, and I have a simple, but hard to solve problem for me.

Well, it's a little bit hard to explain. In short, "Output above of Input".

Let me explain. When you are reading some text, and you want to read the next line, and the next line of text is displayed above input cursor like:

output text example
output text example 2
Input> 

and you press return,

output text example
output text example 2
output text example 3
Input>

the console will look like this.

I tried to google before I posted a question on Stack Overflow, but I couldn't think of a proper keyword to google it. And I'm not sure this explanation is enough for you to understand.

Nevertheless, if you let me know how to make them with C++, it will be appreciated so much.

Regards

donjuedo
  • 2,475
  • 18
  • 28
Kangjun Heo
  • 1,023
  • 1
  • 8
  • 19
  • What do you want to happen instead? – bvpx Feb 13 '17 at 15:27
  • Originally, When input occured, output right next is printed on next line. what I want is printing output on previous line of input. of course, input should be removed before output started. – Kangjun Heo Feb 13 '17 at 15:30
  • This depends on your runtime environment; "the console" is not a C++ concept (all it knows about is stdin and stdout/stderr). For example, on Linux, Bash and various terminal emulators support [ANSI escape sequences for moving the cursor around](http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html). On Windows, [the Win32 API exposes functionality for manipulating the cursor](http://stackoverflow.com/a/34843392/21475). If you want something slightly more portable and robust, look into the curses library. – Cameron Feb 13 '17 at 15:52

3 Answers3

2

Look into nCurses NDK++, it allows console manipulation in C++

http://ndk-xx.sourceforge.net/

I found a good tutorial on YouTube that seems to fit your use-case almost perfectly (it's a 10-video series of using nCurses to make a text-based console game).

https://www.youtube.com/watch?v=3YiPdibiQHA

bvpx
  • 1,227
  • 2
  • 16
  • 33
1

The only way is to make an array and save the output in it and then every time the user input another input you clear the console and update the array with your last output and then print the array output ( which now has 2 outputs inside it ) then wait for the input again ... and when he input you update the array by adding the last output to it then you clear console and print the outputs of array and then wait for input and so on..

here is a link on how to clear the console

NOTE ::

clearing console is not something build in C++ because C++ when it prints it may print to file or printer or any output not just screens. it is an operating system function.

How can I clear console

happy coding

Community
  • 1
  • 1
ahmed nader
  • 354
  • 1
  • 3
  • 13
  • then, should I refresh all of text on every inputs? – Kangjun Heo Feb 13 '17 at 15:26
  • see, on every input you generate the output and put it in the array and then print the array and wait for another input , and he will input again so you make another output and put it on the last one in array and then print the array and then wait for input again and so on . i updated my answer with a link that will help you clear the console – ahmed nader Feb 13 '17 at 15:29
  • This is clearly not "the only way" (see the answer on using nCurses). It is the only way *that doesn't require operating system specific code*. – Martin Bonner supports Monica Feb 13 '17 at 16:34
1

If you look up a set of ASCII codes, you'll see codes like

8 BS backspace

13 CR carriage return

27 ESC escape

Most consoles obey these codes. (You'll have to look up the escapes, they give things like colour and boldness). You can use them to create a more interactive experience, rather than the simple printf() a line / fgets() user line loop.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18
  • When you say "look up the escapes". You'll also have to know what sort of terminal you are talking to (or what sort of terminal the console is behaving like). The escape sequences for an ADM3A, a VT52, and a VT100 are very different. – Martin Bonner supports Monica Feb 13 '17 at 16:35
  • Yes, the escapes aren't standardised, but they do allow some consoles to have very rich displays, built entirely on top of calls for fputc(stdout). – Malcolm McLean Feb 13 '17 at 17:44