Click to See Complete Forum and Search --> : Simple Color


LanWanTanMan
12-11-2000, 05:10 PM
I have to write a program for my computer science class at school, and well I would like to wow them with color. It is a basic program. I am not sure what libraries you have to use or what. There is a colordlg.h library that I have found but I am not sure how to use it. I am a newbie a programing.

#include <iostream.h>
int main()
{
cout << "Hello" << endl;
return(0);
}


What code does I need to put in there to make it so that "Hello" would be red, blue, yellow, green or brown. I hope there is a simple answer! Thanks

If you need to know, I use Borland C++ Complier and/or Micrsoft Visual C++

Fandelem
12-11-2000, 05:42 PM
I never understood, why do you need the return(0); in that statement for it to work? also, what does the return0 state?

EscapeCharacter
12-11-2000, 05:42 PM
i dont know if windows has this but in linux normal stdio.h and iostream.h have escape characters that do color using their hex equiv.

------------------
I like source it never *****es about dependencies
--Escchr 2000

zGoRNz
12-11-2000, 06:41 PM
for linux you can use
WHITE="\033[1;37;40m"
LGRAY="\033[37;40m"
GRAY="\033[1;30;40m"
BLACK="\033[30;40m"
RED="\033[31;40m"
LRED="\033[1;31;40m"
GREEN="\033[32;40m"
LGREEN="\033[1;32;40m"
BROWN="\033[33;40m"
YELLO="\033[1;33;40m"
BLUE="\033[34;40m"
LBLUE="\033[1;34;40m"
PURPLE="\033[35;40m"
PINK="\033[1;35;40m"
CYAN="\033[36;40m"
LCYAN="\033[1;36;40m"
NONE="\033[00m"


these might not work in dos

------------------
Dunt Dunt Duh...
GoRN To The Rescue,
Yet Again
zGoRNz@yahoo.com
aim: GoRNToTheRescue

LinuxAnt
12-11-2000, 06:47 PM
Fandelem
return(0);
is neseccary becaue of the int main(){
statment. This tells the program to expect a returned integer value. some people do it that way, others state it void main(){
this doesn't need a return statement.

LanWanTanMan
12-11-2000, 07:15 PM
I know I do not need the return statement but the book tells us to put it in there and the teacher take off if it is not. So I guess it is a habit. I am still not sure what you all are tell me. Can you write a simple program saying, "Hello" so I can see what it looks like. I like example to look off of when something is explaned. I can see what they are talking about.

Fandelem
12-11-2000, 07:16 PM
But my programs work without using return(0) - is it just "good programmer" habit to start sticking return(0) at the end of my int main()'s?

LanWanTanMan
12-11-2000, 07:18 PM
I guess, programs work fine without it!

Strike
12-11-2000, 07:32 PM
It's actually an ANSI standard. So it's not ANSI compliant code unless you have that little bit there. Plus, it's bad coding practice to start ignoring return values. Only when you have a void return type should you not put a return statement in (and even then, I'm not sure what the standard is).

LanWanTanMan
12-11-2000, 07:48 PM
Does the return(0); have any value to a program at all? Plus please help me with the real question!

Gweedo
12-11-2000, 08:58 PM
#include <iostream.h>

int main()
{
cout << "\033[36;40m" /* this makes text turn Cyan */
<< "Hello" << endl;
return(0);
}


This shows ya how to use the escape key sequences. You can put any of the before mention colors in. Try it out and you will see what happens. Return (0) tells the system that everything went okay in the program. If you do not have it there it will assume everything went okay, but sometimes this is not always the case.

------------------
Dubbie..Dubbie..Do..
Watch out.. or the Penguin will get You http://www.linuxnewbie.org/ubb/wink.gif

[This message has been edited by Gweedo (edited 11 December 2000).]

[This message has been edited by Gweedo (edited 11 December 2000).]

Gweedo
12-11-2000, 09:05 PM
By the way, you can run a search on escape key sequences and get a tons of info on them.

LanWanTanMan
12-11-2000, 09:19 PM
It did not work for me. I tried it under Microsoft Visual C++ and Borland C++ and they both gave me the same output. I am not sure it is windows or not. It might work under Linux but I will be mainly programing in Windows. If you can point to a good forum about windows programing that will help.

Gweedo
12-11-2000, 10:26 PM
The only other thing you could try is use the conio.h header file:


#include <iostream.h>
#include <conio.h>

int main()
{
textcolor(RED);
cout << "Hello" << endl;
return(0);
}



do not know if this will work but give it a run and see. It has been a long time since I have actually used the conio.h header file.

Gweedo
12-11-2000, 10:28 PM
Originally posted by Strike:
Only when you have a void return type should you not put a return statement in (and even then, I'm not sure what the standard is).

Yea, they do not want ya returning anything if it is a void function. Infact, g++ gives ya a warning about having a return in a void function. Basically says, "hey stupid fscker this is a void function, nothing should be returned." http://www.linuxnewbie.org/ubb/tongue.gif

------------------
Dubbie..Dubbie..Do..
Watch out.. or the Penguin will get You ;)

LanWanTanMan
12-11-2000, 10:30 PM
No it did not work. I don't think the conio.h header has the color stuff. There is a header called colordlg.h It has color stuff in it but I am not sure how it works!

jemfinch
12-12-2000, 07:12 PM
Originally posted by LanWanTanMan:
Does the return(0); have any value to a program at all? Plus please help me with the real question!

The return value from main is what the shell is given as a return value for the program. Generally, a non-zero return value indicates error, hence, most people return 0 when there has been no such error.

Jeremy

LanWanTanMan
12-13-2000, 11:54 AM
So basicly you do not need the return(0) at all. I am guess it is there just for looks. If you look at the book "A Giuds to Programming in C++" by Lawrenceville Press it says that you need it for every program. I am going to start not putting the return(0) in there. And yet still no one has answered my one question! Are there any message board for Windows Programing. I can not find any!

Stuka
12-13-2000, 12:20 PM
http://devcentral.iftech.com has some Win32, MFC, general C++ tutorials, plus a Q&A discussion board that is primarily MS/Windows based.

Strike
12-13-2000, 12:44 PM
Originally posted by LanWanTanMan:
So basicly you do not need the return(0) at all.
No.

You do. If you specify int main () then you HAVE TO HAVE a return. You can always return non-zero amounts on fatal errors (of course, within a hello world program not a whole lot of error-checking is generally done). Just do it. It's good practice.

TheLinuxDuck
12-13-2000, 01:07 PM
Ok, everyone has given their bit on the return value, but I don't think it has been made clear.

When you run a program from a shell, the program should return a value to tell the shell if the program ran succesfully, or if there was a problem. That way, the shell can react accordingly.

This is a return value, or error level. Usually a program will return 0 to indicate that the program ran successfully. Not all program follow this, but most do.

Now, you can use the return level in a shell script to handle what the script does next. For example, if you're trying to add a new user in a shell script, and the adduser function returns a 0, you basically know that the adduser function ran correctly. However, if it did not return a 0, your script can react accrdingly to let you know that the addition was not successful, or to do some other checking to find out why the adduser failed. (adduser is just an example, I'm not sure how it deals with error levels)

So, when a program exits, the return value is important.

The part of the C program:

int main

tells the compiled program to return an integer when the program exits. It's up to the coder to determine what return value to use.. as I have said, most programs use 0 to indicate that everything went ok. You can basically return any valid integer, but it's better to stick with the 'standard'. I tend to use 0 for normal operation, 1 for when they don't use the CL options correctly, 2 for not being able to open files, etc, and so on.. it's kinda up to you to determine what happens.

So, in essense, return values are necessary. But, if you were to define main as:

void main

You don't need to return a value, in fact, the compiler will bark at you.. but this isn't a good habit to get into.

Using return values internal to the program are another story altogether...

I hope that I have made return values more clear to you. http://www.linuxnewbie.org/ubb/smile.gif

As for a good windows programming site, good luck. http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq

LanWanTanMan
12-18-2000, 12:19 PM
I was able to get the color to work. I used the borland compiler to make this.

#include <iostream.h>
#include <conio.h>

int main()
{
textbackground(BLUE); // Make the background blue
textcolor(RED); // Makes the text Red
clrscr(); // Refreshs the screen so the color will show up
cout << "Hello!" << endl; //Print out Hello! in the color red
return(0); // Oh look at that!
}


Enjoy!

[This message has been edited by LanWanTanMan (edited 18 December 2000).]