Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 13th, 2005, 12:14 AM   #1
Xenon
Programmer
 
Join Date: Oct 2005
Location: UK
Posts: 31
Rep Power: 0 Xenon is on a distinguished road
Send a message via AIM to Xenon Send a message via MSN to Xenon Send a message via Yahoo to Xenon
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?
Xenon is offline   Reply With Quote
Old Nov 13th, 2005, 1:15 AM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 294
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
Try something like this:

	char color[10] = "Color ";
	char value[2];

	cout << "Hex value: ";
	cin >> value;

	strcat_s(color, value);
	system(color);
andro is offline   Reply With Quote
Old Nov 13th, 2005, 6:58 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 13th, 2005, 7:48 AM   #4
Xenon
Programmer
 
Join Date: Oct 2005
Location: UK
Posts: 31
Rep Power: 0 Xenon is on a distinguished road
Send a message via AIM to Xenon Send a message via MSN to Xenon Send a message via Yahoo to Xenon
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);
but then the value is just 'A0'.

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);

		
}
Xenon is offline   Reply With Quote
Old Nov 13th, 2005, 9:42 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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;
}
This was only cursorily tested. Your mileage may vary. Your warranty expired day before yesterday.
__________________
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
DaWei is offline   Reply With Quote
Old Nov 13th, 2005, 9:47 AM   #6
Xenon
Programmer
 
Join Date: Oct 2005
Location: UK
Posts: 31
Rep Power: 0 Xenon is on a distinguished road
Send a message via AIM to Xenon Send a message via MSN to Xenon Send a message via Yahoo to Xenon
Thankyou very much DaWei, thats great.
Xenon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC