Search code examples
c++textcolorturbo-c++conio

How to pass user input color into textcolor()?


How do I pass a user input color to the textcolor() function in conio.h ?

textcolor(BLUE);
cprintf("Hello");

works fine, but

char c[20];
gets(c);
textcolor(c);
cprintf("Hello");

throws an error. I didn't expect it to work myself. So the question is, how can a user enter a color for text to be displayed? Due to some stupid constrains, I have to do this on the old turbo c++ and cannot use graphics.h , dos.h etc. So a solution that uses textcolor() itself would be great.


Solution

  • Your code will look something like this:

    char c[20];
    gets(c);
    
    if (strcmp("BLACK",c) == 0) {textcolor(BLACK);}
    else if (strcmp("BLUE",c) == 0) {textcolor(BLUE);}
    else if ... more colors here ...
    
    cprintf("Hello");
    

    Remember the BLUE is not a string, it is a macro that equals the integer value of 1. gets() returns a string hence the strcmp() function.