Well, the question may sound a bit too vague but here's 2 things I need to do and I'd definitely need some input on this :
Output something (e.g. using cout) with color (note: My TERM environment variable is set to xterm-color if that makes any difference; also, is there any uniform way to output colored text that's compatible with both pure mac and *nix terminals in general, so that the code is portable)
Output something at the same position on the terminal screen. OK, this may sound confusing too. Let's take a terminal app which simply outputs a progress percentage. It normally won't start a new line for that. The new value is shown at the very same spot. How is this doable? (Being a once Borland Pascal guy from the good old DOS days, the only thing I could think of is something to do with accessing video memory directly... or not?)
So... any ideas?
You probably want to use ncurses library. And ANSI escape codes can also be used for coloring.
1)
You can try Color cout , but that is not protable. I tried (ANSI escape codes) something like
cout << "\033[1;31mbold red text\033[0m\n";
cout << "\33[0;31m" << "Enter Your String here" << "\33[0m" << std::endl ;
You can also look at
How do I output coloured text to a Linux terminal?
2)
Are you looking for something like watch or top like app which are showing output at the same spot.
Related
I'm a beginner coder making a simple text 'choose your own adventure' game, but I want it to scroll out the text like an RPG instead of just spitting out the text. How can I do that?
I believe that the ncurses library is exactly what you are looking for. It allows you lower access to the terminal text, to produce things like full screen text, like this:
A tutorial for how to use it can be found here, and you can download version 6.3 here.
It is used in many applications, like GNU nano. A full list of applications using ncurses can be found here.
I'm going to assume you're just supposed to write to a console.
std::string str = "Your text";
for(char& current_char : str) {
std::cout << current_char << std::flush;
sleep(1000);
}
std::cout << std::endl;
The for loop is going to iterate over each character in the string.
It will then output it to cout. std::flush is here to force the output to be update without using std::endl which would return carriage and we don't want that.
sleep will pause for an amount of time in milliseconds, here 1000 milliseconds (1 second). Be careful though; sleep is a Windows function. On Linux, use usleep. If you need it to be cross-platform you should probably use the thread sleeping functions or make something yourself with chrono.
And finally, we use std::endl to return carriage and update the output.
This question already has answers here:
C++ Update console output
(4 answers)
Closed 5 years ago.
I'm developing some application in which I want to manipulate some data comming from the embedded system. So, what do I want to do is that I want to display the values which are comming on the same position where they were before, leaving the static text on the same place and not using new line. Being more specific, I want to output my data in form of a table, and in this table on the same positions I want to update that data. There is some analogy in Linux, when in the terminal there is some update of the value(let's say some progress) while the static text remains and only the value is changing.
So, the output should look like this:
Some_data: 0xFFFF
Some_data2: 0xA1B3
Some_data3: 0x1201
So in this case, "Some_data" remains unchanged on the same place, and only the data itself is updated.
Are there maybe some libraries for doing that? What about Windows Console Functions? Also, it would be very nice if it could be made in such a way, in which the console would not flick, like when you clear the console and print something back. Any hints or suggestions? Thanks very much in advance guys!
P.S. There is no need to write the code, I just need some hints or suggestions, with very short examples if possible(but not required).
On a *nix system you have two options.
1) If you want to manipulate the entire console in table form like you ask, then ncurses is the best option. The complete reference can be found here.
As you can see, that package is quite heavyweight and can often be overkill for simple projects, so I often use . ..
2) If you can contain your changing information on a single line, use the backspace escape char \b and then rewrite the information repeatedly to that line
For example, try this . . .
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
void writeStuff(int d)
{
cout << string(100,'\b') << flush;
cout << "Thing = " << d;
}
int main()
{
cout << "AMAZING GIZMO" << "\n============" << endl;
while(1) {
writeStuff(rand());
this_thread::sleep_for(chrono::milliseconds(250));
}
}
For a real world example, the sox audio console playback command uses this technique to good effect by displaying a bar chart made of console characters to represent the audio playback level in real time.
Of course, you can get more creative with the method shown above if your console supports ANSI escape sequences.
My compiler is Turbo C++ v3.0 with DOS v5.0 emulated in DOSBox v0.74
I use this because Turbo C++ is the compiler with which
my highschool has chosen to teach the C++ programming language.
It has been stressed that I use this compiler while coding my final term project.
I'm running Windows 8.1 (64 bit) with Intel Core i5-3317U CPU # 1.70GHz
For the sake of liveliness and in tribute to popular culture,
I want my output screens to have green text.
The following is what seemed to work :
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
textcolor(2); // text set to green colour (conio.h function)
cprintf("\n\t Hello World"); // cprintf from conio.h
cout << "\n\t Hello World"; // cout from iostream.h
getch();
}
The output of which is as follows (screen has been trimmed to save space on this post):
According to the Help Section in Turbo C++,
cprintf() sends formatted output to the text window on the screen.
As you can see, the text printed onto the screen by cout is not green and my project is composed of a lot of cin and cout and some writing and reading files.
The result I desire can (although I have not yet tried) most likely be obtained by replacing all my cout << "..."; with cprintf("...");
but I've written so many cout statements that it's going to be hard to edit the code that much.
cprintf is new territory for me and I'm slightly set aback by how
cprintf("\t"); is outputed as o
So, I'm not to keen using this. I don't wish to use this until and unless it is my only option.
The libraries cstdlib.h and windows.h are unavailable in Turbo C++ and hence I can't use their utilities to get what I want either.
In the end, all I want is that output prompt to display the text I've couted in bright green. Minimal change to my code would be nice.
I wouldn't even mind having to change some settings of my emulator or compiler or shell to do it.
All help is much appreciated. Thank you in advance =)
Ah, the 1990s called, they want their QEMM back :)
The one solution I can think of is to put this in your CONFIG.SYS:
DEVICE=C:\DOS\ANSI.SYS
and then output ANSI escape sequences.
You can use the constream library for colored text output:
#include <constrea.h>
int main()
{
constream cout;
cout << setclr(2);
cout << "\n\t Hello, World!" << endl;
getch();
return 0;
}
I don't know what to do about the tab character.
you just need to add the clrscr(); function after the textcolor(); and it works with the couts
I'm curious how one handles output in languages (e.g. Arabic, Hebrew) that are read from right to left. Almost everything in C/C++ or any language I have seen is premised on outputting from left to right.
Is there a system call or something that will force a terminal, for example, to print right to left? Even so, inside a program strings are coded like string("this is a string"). Wouldn't it have to code something like string("string a is this") or some reverse function be used one everything?
I suspect that in practice, a system that is configured for a right-to-left language will have terminal settings the print from right-to-left, so that the internals of a C/C++ program need not worry about it.
If that doesn't reassure you, here's a hack:
string str = "The look on your face is priceless.";
cout << "\u202e" << str << endl;
At the lowest layers, there are the extended window styles for setting up a window in a "right to left" ordering: WS_EX_LAYOUTRTL, WS_EX_RIGHT, and WS_EX_RTLREADING. Also, the API call, SetProcessDefaultLayout with LAYOUT_RTL. The right combination of these flags and APIs means the (0,0) coordinate is the top right of the window and the x-axis increases linearly.
Here's a link to the classic MSDN article that explains all the fine details.
I'd like to have my program output "cm2" (cm squared).
How do make a superscript 2?
As Zan said, it depends what character encoding your standard output supports. If it supports Unicode , you can use the encoding for ²(U+00B2). If it supports the same Unicode encoding for source files and standard output, you can just embed it in the file. For example, my GNU/Linux system uses UTF-8 for both, so this works fine:
#include <iostream>
int main()
{
std::cout << "cm²" << std::endl;
}
This is not something C++ can do on its own.
You would need to use a specific feature of your console system.
I am not aware of any consoles or terminals that implement super-script. I might be wrong though.
I was trying to accomplish this task for the purpose of making a quadratic equation solver. Writing ax² inside a cout << by holding ALT while typing 253 displayed properly in the source code only, BUT NOT in the console. When running the program, it appeared as a light colored rectangle instead of a superscript 2.
A simple solution to this seems to be casting the integer 253 as a char, like this... (char)253.
Because our professor discourages us from using 'magic numbers', I declared it as a constant variable... const int superScriptTwo = 253; //ascii value of super script two.
Then, where I wanted the superscript 2 to appear in the console, I cast my variable as a char like this...
cout << "f(x) = ax" << (char)superScriptTwo << " + bx + c"; and it displayed perfectly.
Perhaps it's even easier just to create it as a char to begin with, and not worry about casting it. This code will also print a super script 2 to the console when compiled and run in VS2013 on my Lenovo running Windows 7...
char ssTwo = 253;
cout << ssTwo << endl;
I hope someone will find this useful. This is my first post, ever, so I do apologize in advance if I accidentally violated any Stack Overflow protocols for answering a question posted 5+ years ago. Any such occurrence was not intentional.
Yes, I agree with Zan.
Basic C++ does not have any inbuilt functionality to print superscripts or subscripts. You need to use any additional UI library.
std::cout << cm\x00B2;
writes cm^2.
For super scripting or sub scripting you need to use ascii value of the letter or number.
Eg: Super scripting 2 for x² we need to get the ascii value of super script of 2 (search in google for that) ie - 253. For typing ascii character you have to do alt + 253 here, you can write a any number, but its 253 in this case.
Eg:-cout<<"x²";
So, now it should display x² on the black screen.
Why don't you try ASCII?
Declare a character and give it an ASCII value of 253 and then print the character.
So your code should go like this;
char ch = 253;
cout<<"cm"<<ch;
This will definitely print cm2.