How to output above input line? - c++

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

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

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

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.

Related

Allow a user to not only input multliline text but move up and edit previously entered lines

Im working on a crude console based text editor in C++ kind of like nano. I've already figured out a basic way of inputting multiple lines of text and writing it to a file correctly (input ends when they enter a code: //end). However, at the moment the user is unable to move upwards (using arrow keys) and edit lines that they have entered. For some additional information I'm doing this with a getline() loop, writing files with ofstream, and am storing the users text in a string vector with each element being an entered line. How might I implement the ability to work with a body of text in such a way?
For advanced use, you need access to the console API.
For a simpler version, look at the primitive visual editors.
Editing a line consists of moving to the line, printing out the content, and then letting them insert or delete characters on that line.
Look at sed or ed or even vi.

How can i write text in c++ console without breaking the lines

how can i input text in c++ console without breaking the input in one line at a time?
If i use cin i can input one string each time plus i cannot edit the input (except if i edit the string but this wont help)
Is there any way to input strings (with multiple lines) but not to break the string in one line at a time?
I am running ubuntu 12.04
Who is writing? Is it you, or some program??
Your terminology is unusual: generally programmers take the point of view of the computer!
What you write by typing on your keyboard is an input to some program (which reads it).
If you want an editable input (to the program, so "written" or typed by the human user), consider using GNU readline (on Linux), or perhaps ncurses
If you want to format the program's output (which the user would read with his eyes), you'll generally need to code that formatting explicitly. Perhaps ANSI escape codes might be useful (but using them might make readline or ncurses unhappy).
See also this answer and the references I gave there.

How to keep characters in C++ from combining when outputted to text file

I have a fairly simple program with a vector of characters which is then outputted to a .txt file.
ofstream op ("output.txt");
vector <char> outp;
for(int i=0;i<outp.size();i++){
op<<outp[i]; //the final output of this is incorrect
cout<<outp[i]; //this output is correct
}
op.close();
the text that is output by cout is correct, but when I open the text file that was created, the output is wrong with what look like Chinese characters that shouldn't have been an option for the program to output. For example, when the program should output:
O dsof
And cout prints the right output, the .txt file has this:
O獤景
I have even tried adding the characters into a string before outputting it but it doesn't help. My best guess is that the characters are combining together and getting a different value for unicode or ascii but I don't know enough about character codes to know for sure or how to stop this from happening. Is there a way to correct the output so that it doesn't do this? I am currently using a windows 8.1 computer with code::blocks 12.11 and the GNU GCC compiler in case that helps.
Some text editors try to guess the encoding of a file and occasionally get it wrong. This can particularly happen with very small amounts of text because whatever statistical analysis is being used just doesn't have enough data to make a good conclusion. Window's Notepad has/had an infamous example with the text "Bush hid the facts".
More advanced text editors (for example Notepad++) may either not experience the same problem or may give you options to change what encoding is being assumed. You could use such to verify that the contents of the file are actually correct.
Hex editors/viewers are another way, since they allow you to examine the raw bytes of the file without interpretation. For instance, HxD is a hex editor that I have used in the past.
Alternatively, you can simply output more text. The more there is, generally the less likely something will guess wrong. From some of my experiences, newlines are particularly helpful in convincing the text editor to assume the correct encoding.
there is nothing wrong with your code.
maybe the text editor you use has a default encoding.
use more advanced editors and you will get the right output.

Standard console output without nextline

I would like to write 80 (standard conole width) characters in one line without the cursor go to next line. It is only problem when I want to print 80 characters in the last line of console. It cases scrolling that I dont want.
Take a look:
I dont want the newline. any way to do this? :/
Im on Windows, DEV-C++, using WinApi for colors and moving the cursor (the window resize too).
Thanx for any answers.
Instead of using standard output functions use the Windows Console API to set the cursor position and draw characters. Specifically, take a look at WriteConsoleOutput.
MSDN Console API Docs
The only reason why you are on a new line is because the console is not big enought to support the eighty stars.
So it pushed the cursor to the next line.
through one or two "\b" at the end it moves the cursor back.
For the system-critical console window, the cursor should always stay visible, and the only way for it to do so after you've reached the max number of chars in a line, is to pop up on the next visible line (without actually making any new lines).
filter the output either in the originating program or with another program through a pipe. When you have outputted too many characters on a single line, do whatever you like (i.e., drop characters, overwrite, etc....).

C++ standard output format

I want to create a C++ console application that print some text to different parts of the console. For example in QBasic you can use:
locate(8,5)
print "hi"
And hi would be printed in column 8 line 5. In C++ when I use cout it always prints on the next line, and begins printing in the first column.
Is there any way I can do this?
C++ itself does not have this feature, it's I/O model is a fairly simple, sequential one.
If you want to do fancy cursor positioning, you'll need to output (for example) control characters which your terminal will recognise as special commands (such as ANSI or VT escape sequences), or use a library like curses (see ncurses here) which can do a lot of the grunt work for you, not just cursor positioning but also things like text mode windows and so forth.
A library, like ncurses can help you do this.