C++ standard output format - c++

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.

Related

How to output above input line?

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.

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.

Linux Printing - How To

I find it hard to explain but I will try my best. Some times in Linux- in the Terminal- things get printed but you can still write over them. eg when using wget you get a progress bar like this:
[===================> ]
Now if you type something while it is doing this it will 'overwrite' it. My question is how to recreate this in c++.
Will you use something like
cout <<
or something else?
I hope you understand what I am getting at...
btw I am using the most recent version of Arch with xfce4
Printing a carriage return character \r is typically interpreted in Linux as returning you to the beginning of the line. Try this, for example:
std::cout << "Hello\rJ";
The output will be:
Jello
This does depend on your terminal, however, so you should look up the meaning of particular control characters for your terminal.
For a more cross-platform solution and the ability to do more complex text-based user interfaces, take a look at ncurses.
You can print the special character \b to go back one space. Then you can print a space to blank it out, or another character to overwrite what was there. You can also use \r to return to the beginning of the current output line and write again from there.
Controlling the terminal involved sending various escape sequences to it, in order to move the cursor around and such.
http://www.ibiblio.org/pub/historic-linux/ftp-archives/tsx-11.mit.edu/Oct-07-1996/info/vt102.codes
You could also use ncurses to do this.

How to program a spinning cursor in c++ on OSX?

I need a progress indicator in the form of a spinning cursor for a command line program. I read a couple of threads with the advice of using the backspace character \b to delete the last printed character. However, when using \b with cout on OSX, the result is some UTF-8 character (an upside-down question mark).
Does anyone know if there is a way to get this problem solved with standard c++ means?
You can use the ncurses library for cross-platform terminal access. You csn make nice text-only UIs using this.