colour to font and background

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# include <iostream> 
using namespace std;

int main ()


{
    int c;
    do
    {
int a,b,c ;
cout <<" enter ur first no. " << endl;
cin >> a; 
cout  <<" enter ur second no. " << endl ;
cin >> b ;
cout <<" ur ans is " << (a+b) <<endl;
}
while (0==0);
scanf ("%d%",&c);
return 0;
}

take this simple program in consideration..how can i give colors to fonts and background ?
Last edited on
Why did you create a new post? You already posted the same problem. In your previous post I showed you how to do what you're asking about continuing the program. I'm browni3141. Check my first reply under your old post.
ok :TOPIC EDITED: plz look in my last topic i have another problem
Last edited on
What you have above is pretty close. All you need to do is declare a variable to use for user input. Then get input from the user, such as 'y' or 'n'. Finally fix your while statement to say [code]while ((input == 'N') || (input == 'n'))[/cod]
|| is a relational operator that means "or".

EDIT: You can change the colors by right clicking the top of the window, selecting properties and going to colors.
Last edited on
what i mean by giving colors is how to give them while programming..means when i code this program this will appear in black background with white text..so how can i give different color to my program
I believe this is what you are looking for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream.h>
#include <conio.h>

// Needed for the SetConsoleTextAttribute () Function
#include <Windows.h>

int main ()
{
    using namespace std;
    
    // Set the default text
    SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 7);
    
    // Get input
    int iInput;
    cout << "Enter a number and I will give you a color: ";
    cin >> iInput;
    
    // Clear the screen
    system ("cls");
    
    // Set the color
    SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), iInput);
    
    // Print text
    cout << "Color Number: " << iInput << endl << endl;
    cout << "Code To Get This Color:" << endl;
    cout << "SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), " << iInput << ");";
    getch ();
    
    // Clear the screen and re-run the program
    system ("cls");
    return main ();
}
Obligatory link about system() calls: http://www.cplusplus.com/forum/articles/11153/

And don't ever call main(). It doesn't even make sense.
thanks buddy but what about the background screen
@im abcd: don't get to hung up about the looks of your console programs. Learning console tricks is too specific and won't help you when you make the move to GUI.
lol I doubt I piece of code is "evil", and return main () makes since to me, when you call a function in a return statement it is going to call the function when it returns, it works for me....

If you get into high enough numbers then it starts to color around the text, and the background, try some things out. Also filipe is right, don't get into colors with console applications, waste of time. In my opinion console applications are only to be used to learn basics of C/C++, then once you get into Win32 programming you can do a lot of things with actual windows, and not just things in the MSDOS window.
lol I doubt I piece of code is "evil"

Have you read anything besides the title? It's bad practice.

and return main () makes since to me, when you call a function in a return statement it is going to call the function when it returns, it works for me....

That program, as it is, will run forever and recursively call main() until the stack overflows (if it compiles and runs at all, that is; calling main() is probably not allowed by the standard). When you have a statement such as

return foo();

function foo() will be executed and, when it returns, its return value will be used by the statement.
Actually, return main(); is forbidden by C++: You are not allowed to take the address of main() or call it as a function()
either directly or indirectly (via pointer).

When I use it it returned back to the main function ....
Then your compiler is stupid.

Don't ever call main()!

(Use a loop.)



Set colors on Windows:
http://www.google.com/search?btnI=1&q=msdn+SetConsoleTextAttribute
It works for me on Dev-C++, Code::Blocks, and VC++ 2010....
CPPProgrammer4L wrote:
When I use it it returned back to the main function ....

The return statement is not analogous to a goto. Your program is not sending control back to the first line of main(); it's actually running a new instance of main() every time it executes that statement. The program will never terminate on its own, and if you keep going for long enough, the stack will overflow. Are you familiar with recursion? Basically, your main() function never ever returns, it just keep recursively calling itself.

It works for me on Dev-C++, Code::Blocks, and VC++ 2010....

That doesn't make it any more correct. And don't use Dev-C++, it hasn't been updated in 5 years.
CPPProgrammer4L wrote:
It works for me on Dev-C++, Code::Blocks, and VC++ 2010....
So what?

It is forbidden by the standard.

Just because it doesn't immediately do evil things to you (or your users) on Windows compilers doesn't make it OK.

Remember, main() is not a normal function!
http://www.cplusplus.com/forum/beginner/5878/

Hope this helps.
Last edited on
closed account (S6k9GNh0)
Or we could go towards portability and throw away conio and the WinAPI in exchange for a curses (preferably ncurses) library but that's just me.

Also, Code::Blocks and Dev-C++ are not compilers and VC++ is only by technicality.
Last edited on
Also, Code::Blocks and Dev-C++ are not compilers and VC++ is only by technicality


What do you mean they're not compilers? If they're not compilers what are they? And what makes VC++ so special?
They're IDEs. VC++ has its own compiler, while Code::Blocks and Dev-C++ use MingW IIRC.
Pages: 12