I am trying to write a program that takes a string from the user and formats the CLI text using an ANSI escape code. Below is the code I have got closest to work, as you can see I have broken the ANSI code into single characters to try and input the different color codes.
The escape code is currently appearing as a string through cout, I think it is because the integer which choses the colour is not the same variable type as the rest of the character array.
Further, I am getting a segmentation fault that I can not source.
Any help is greatly appreciated, I would like someone to simply point me in the right direction to give me a chance to try and understand why this is not working.
When you write a string literal a null character is automatically added to the end so there is no need to write \0 there unless you want two null characters at the end of the string for some reason.
You should initialize the num elements to 0 because that is what your code assumes. This is probably the cause of the segfault.
On line 36 you want to convert i into a character. Just using a cast will not work because the character values of the digits '0'-'9' are not the same as the corresponding integer values 0-9. One way to do the conversion is to add the zero character '0' and the number. code[ j ][5] = '0' + i;
Thank you very much,
I have adapted the script and simplified it considerably.
Initialising the elements of N corrected the segfault.
As for using ANSI - the day I go back to Windows is the day I sell my soul to Microsoft.
I may include an ifdef statement and use the console API to make it fully portable, however, this was just a little project for no practical application other than to develop my string handling in c++ so I may not.