1

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.

Aswin G
  • 97
  • 2
  • 8
  • It's important to understand that `BLUE` is not a string, where the value read using `gets` is a string. You need to convert that string to a value, perhaps using a lookup table, or a series of `if` statements. – CAB Sep 27 '16 at 15:57
  • Also, when code `throws an error`, include the error information in your question. – CAB Sep 27 '16 at 16:00

1 Answers1

1

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.

Gerhard
  • 6,850
  • 8
  • 51
  • 81