![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
Changing Console colour dynamically
Hi, Ive written the following function in the attempt to allow the user to be able to change the console colour whenever they want, but I dont know how to pass the two seperate HEX digits to the system, I tried using:
system("Color &colour");But that didnt work, heres my function: void colour()
{
char colour[3];
char inbuf[30];
printf("\nColour attributes are specified by TWO hex digits -- the first");
printf("\ncorresponds to the background; the second the foreground. Each digit");
printf("\ncan be any of the following values:\n\n");
printf(" 0 = Black 8 = Gray\n");
printf(" 1 = Blue 9 = Light Blue\n");
printf(" 2 = Green A = Light Green\n");
printf(" 3 = Aqua B = Light Aqua\n");
printf(" 4 = Red C = Light Red\n");
printf(" 5 = Purple D = Light Purple\n");
printf(" 6 = Yellow E = Light Yellow\n");
printf(" 7 = White F = Bright White\n");
printf ("\nPlease enter the two Hex digits that correspond to the colours\n");
printf ("you would you like the backround and foreground to be:");
if(gets(colour) == NULL) ;
sscanf(inbuf,"%colour",&colour);
system("Color &colour");
app_settings();
}Can anyone help? |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Try something like this:
char color[10] = "Color "; char value[2]; cout << "Hex value: "; cin >> value; strcat_s(color, value); system(color); |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Andro might have read your mind, but remember that you probably need to specify a platform/OS for things like this. Won't work on a TTY at all, for instance.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Programmer
|
Im on Win XP, and Im using ansi C so Cout and Cin dont work (atleast I dont think they do Ive only been learning this for a short time)
With the following code, if I enter for example 'A0', the value stored in colour is "A0A0lolo", I cant work out why this is happening. I tried using strncat(colour,value, 2); void colour()
{
char colour[10] = "Color ";
char value[2];
char inbuf[30];
printf("\nColour attributes are specified by TWO hex digits -- the first");
printf("\ncorresponds to the background; the second the foreground. Each digit");
printf("\ncan be any of the following values:\n\n");
printf(" 0 = Black 8 = Gray\n");
printf(" 1 = Blue 9 = Light Blue\n");
printf(" 2 = Green A = Light Green\n");
printf(" 3 = Aqua B = Light Aqua\n");
printf(" 4 = Red C = Light Red\n");
printf(" 5 = Purple D = Light Purple\n");
printf(" 6 = Yellow E = Light Yellow\n");
printf(" 7 = White F = Bright White\n");
printf ("\nPlease enter the two Hex digits that correspond to the colours\n");
printf ("you would you like the backround and foreground to be:");
if(gets(value) == NULL) ;
sscanf(inbuf,"%value",&value);
strcat(colour,value);
printf("%s", colour);
system(colour);
} |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Cin and cout are C++ thangys. This is the C forum.
One presumes this is for practice, since there's a built-in command to do it. That means there are many ways. This particular method takes its arguments from the command line. It differs slightly in that it allows the arguments to be specified as a single-argument pair, or as two separate arguments. There is a modicum of validation. If you wished to take keyboard input and deal with it as hex digits (rather than encoded alphanumeric characters), you might use something like 'scanf ("%x%[, ]%x", &dig1, &dig2);' This would be good practice. You must test the return from scanf, because you can't guarantee what your user will enter. If you received one assignment, you could validate for a packed pair; if you received two assignments, you could validate for individual digits. Anything else would constitute an error. As Larry Wall says, "TMTOWTDIR" (there's more than one way to do it right). Unfortunately, there's more than one way to do it wrong, too. #include <stdio.h>
#include <string.h>
int ohDratMartha (char *badNews)
{
fprintf (stderr, "%s\n", badNews);
return 1;
}
int isColor (char hexdigit)
{
int i;
char *validDigits = "0123456789abcdef";
hexdigit = tolower (hexdigit);
for (i = 0; i < strlen (validDigits); i++)
if (validDigits [i] == hexdigit) return 1;
return 0;
}
int main (int argc, char *argv [])
{
char color [9] = "color ";
if (argc < 2) strcat (color, "0f"); // default
else if (argc == 2)
{
// Packed pair
if (!isColor (argv [1][0]) ||
!isColor (argv [1][1]) ||
(strlen (argv [1]) > 2))
return ohDratMartha ("Invalid color specification");
strcat (color, argv [1]);
}
else if (argc > 3)
return ohDratMartha ("What the hell are the extra arguments for?");
else
{
// Separate arguments
if ((strlen (argv [1]) > 1) ||
(strlen (argv [2]) > 1) ||
!isColor (argv [1][0]) ||
!isColor (argv [2][0]))
return ohDratMartha ("Invalid color specification");
strcat (color, argv [1]);
strcat (color, argv [2]);
}
system (color);
return 0;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Programmer
|
Thankyou very much DaWei, thats great.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|