Coloured output in Turbo C++ - c++

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

Related

C++ and PowerShell: std::cout weird spacing with ANSI colors

I'm facing a weird issue with std::cout, ANSI color codes and PowerShell (integrated terminal in VS Code).
I have written a sample program that asks the user for string input. In the prompt I make use of ANSI color codes, so the default value (in case the user doesn't input anything) is displayed gray instead of white.
Running the executable works as intended the first time, but when running it a second time there's a weird spacing that disappears after the user presses Enter.
This issue persists until the code is compiled again.
I have noticed that when I leave out the ANSI color codes, this spacing also disappears. I'd still like to use colors though.
Please see below for sample code and images showing the output.
What causes this issue? Is there a different way of using colored std::cout output in C++?
Thanks a lot for your time.
Sample code
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "Input: \x1b[37;2m(default value)\x1b[0m ";
std::getline(std::cin, input);
if(input.empty()) {
input = "default value";
}
std::cout << input << std::endl;
return 0;
}
Running the executable for the first time
Running the executable thereafter
Additional information
OS: Windows 10
Terminal: VS Code Integrated PowerShell
C++ compiler: g++ 8.1.0

Why system("color 3") is "global"?

Code:
int print()
{
system("color 2"); //paint the "one"
std::cout << "one" << std::endl;
}
int main()
{
print(); //prints "one"
system("color 3"); //paint the "two"
std::cout << "two" << std::endl;
return 0;
}
I'm a newbie programmer, and I want to write a simple code which prints text in different colors. The thing is, when I compile it, cout “one” and “two” are the same color as I don't expect. However, when I change the color in int main(), cout “one” take color from int main() forgetting about the “color2” from function print().
And my question is, how to avoid that “global” system(“color 3”)? What exactly happened? Is it a good method to color a text?
I want to write a simple code which prints text in different colors.
Your question is not C++ related and is operating system (and hardware) specific. Some computers don't have color screens (and some don't have any screens at all, but still provide a standard conforming C++ implementation, e.g. some web, network, internet server, IoT device, etc...)
We don't know what color is or does. It could even be some of your private program somewhere in your $PATH (used by the command processor working for system). BTW, practically speaking, running an external program to simply change colors is inefficient.
Standard C++ does not know about colors at all. Only about standard streams.
Maybe your program is outputting on some terminal capable of ANSI escape codes (but you need to check that - and with command pipelines or redirections your stdout or std::cout is not even a terminal!). Then you might use something like
#define ESCAPE_BOLD "\033[1m"
#define ESCAPE_NORMAL "\033[0m"
and later
std::cout << "Here is something " << ESCAPE_BOLD << "in bold"
<< ESCAPE_NORMAL << std::endl;
I leave you to find out the escape sequence for red.
Maybe you want to code some GUI application. Then use a widget toolkit like Qt.
Perhaps you want to code a textual interface. Then consider some library for that, e.g. ncurses.
Maybe you want some web interface (so learn more about web technologies), then use some HTTP server library like Wt or libonion.
I assume your color refers to builtin in windows cmd.exe (or DOS builtin). If so, the documentation explains it clearly
The COLOR command will change the color of all the text in the window.
https://ss64.com/nt/color.html
So what happens is
You change the window color to green
Print "one"
Change the window color to aqua - this changes also already printed "one"
Print "two"
You can check if I'm right by adding some delay between changing color (like std::cin.get())

in c++, is it possible to / how can i display console output after input, on same line, before user's input is given?

In C++, when writing to and taking information from the console using cout / cin, is it possible to do something like
ENTER YOUR DATA HERE --> __ <-- ENTER YOUR DATA HERE
With the user input cursor where the underscores are located, and output located on either side of the cursor, and then the user's typed input appearing between those two bits of output before being returned to cin?
If so how would I got about doing that?
I hope that between my title and explanation here that it's clear what I'm asking, if not I can try to explain further.
Ideally I'd like to do this using iostream / cin & cout, because those are what I've used in the past. If the solution is to use... printf or similar I'll do that but may need a bit of additional explanation since I'm not really experienced in using that for output.
NOTE: I tried to find an answer for this problem and I can't say for sure that there wasn't one, it was mostly just a matter of finding a huge amount of other input/output-related questions.
EDIT: This is using the DOS shell on Windows 7, compiling from the Windows Visual Studio 2012 command-line compiler.
If Windows, use the Console API. http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx
If Linux, use the curses library: http://en.wikipedia.org/wiki/Ncurses
If you are using windows you have the option to use the conio.h.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
gotoxy(5,3);
cout<<"HELLO WORLD";
gotoxy(60,10);
cout<<"HELLO WORLD";
gotoxy(35,20);
cout<<"HELLO WORLD";
getch();
return 0;
}
If you wish to use ANSI libraries only, you can try to take a look at this discussion:
http://www.cplusplus.com/forum/lounge/78225/
In both cases all you have to do is print the first text, then use gotoxy, then you print seconde text, gotoxy again where you want the input to happens, and read the user input.

Beginner's Mistake

I know this is probably a dumb question but I am a beginner and I just started learning today. I am using Dev C++ and I wrote my first code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" ;
return 0;
}
I click to compile and run. Nothing comes up. Then I click just "run" and it said it is not compiled yet.
I think there might be errors and I would have gladly fixed them myself but I don't know where I can see the errors in Dev C++.
Could this be a compiler error or did I mess up something in my code?
Thanks!
Most likely your program exits before it manages to write everything out to the console. Try adding new line to the output, like this:
cout << "Hello World" << endl;
When you write to cout, the data is not transfered to the screen right away, out of efficiency considerations. Writing to the screen is relatively slow, so the program prefers to do it in "bursts". The text is accumulated in a buffer until a special command is given to flush the buffer, or the buffer fills up. Writing out endl forces a flush, so the output will appear on the screen before your program exits.

Basic terminal output using C++ - Questions

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.