简体   繁体   English

Turbo C ++中的彩色输出

[英]Coloured output in Turbo C++

My compiler is Turbo C++ v3.0 with DOS v5.0 emulated in DOSBox v0.74 我的编译器是Turbo C ++ v3.0,在DOSBox v0.74中模拟了DOS v5.0。
I use this because Turbo C++ is the compiler with which my highschool has chosen to teach the C++ programming language. 我之所以使用它,是因为Turbo C ++是我的高中生选择用来教授C ++编程语言的编译器。 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 我正在使用Intel Core i5-3317U CPU @ 1.70GHz运行Windows 8.1(64位)

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++, 根据Turbo C ++的“帮助”部分,

cprintf() sends formatted output to the text window on the screen. cprintf()将格式化的输出发送到屏幕上的文本窗口。

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. 如您所见, cout打印在屏幕上的文本不是绿色,我的项目由很多cincout以及一些读写文件组成。

The result I desire can (although I have not yet tried) most likely be obtained by replacing all my cout << "..."; 我希望的结果(尽管我还没有尝试过)很可能可以通过替换所有的cout << "...";来获得cout << "..."; with cprintf("..."); cprintf("..."); but I've written so many cout statements that it's going to be hard to edit the code that much. 但是我写了很多cout语句,以至于很难编辑那么多代码。

cprintf is new territory for me and I'm slightly set aback by how cprintf("\\t"); cprintf对我来说是新领域,我对cprintf("\\t");感到有些惊讶cprintf("\\t"); is outputed as o 输出为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. Turbo C ++中不提供库cstdlib.hwindows.h ,因此我也无法使用它们的实用程序来获取我想要的东西。

In the end, all I want is that output prompt to display the text I've cout ed in bright green. 最后,我只需要输出提示以明亮的绿色显示我cout的文本。 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. 我什至不介意必须更改仿真器或编译器或Shell的某些设置来执行此操作。

All help is much appreciated. 非常感谢所有帮助。 Thank you in advance =) 预先谢谢你=)

Ah, the 1990s called, they want their QEMM back :) 啊,1990年代打来的,他们想要他们的QEMM :)

The one solution I can think of is to put this in your CONFIG.SYS: 我能想到的一种解决方案是将其放在您的CONFIG.SYS中:

DEVICE=C:\DOS\ANSI.SYS

and then output ANSI escape sequences . 然后输出ANSI转义序列

You can use the constream library for colored text output: 您可以将constream库用于彩色文本输出:

#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(); 您只需要添加clrscr(); function after the textcolor(); 在textcolor()之后的函数; and it works with the couts 它与cout一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM