Multiple ASCI

Pages: 12
I'm writing a function that will take an 3 ints 1 char* 1 bool
It takes an int that stands for background color,
One that stands for text color,
And one for special features(bold, underline, etc.)
The char* is an array that is to be displayed this way...
The bool is to make a box surrounding the words 1 if you want box 0 if not
Everything works pretty well except the fact that
I don't know how to use multiple ASCI codes
Btw everything I learned about them I got from here
1
2
3
4
5
6
  #define RED "\033[7;32m"
  #define GREEN "\033[0;33m"
//later in program
printf(RED);//works
printf(GREEN);//doesn't display???
printf("Hi\n!");

Now I know I could do
 
printf( RED GREEN "Hi!\n");

The problem with that is I am not just writing this in main()
I'm putting it in another function...
1
2
3
4
if (background==red && text==green && special==none && box==none)
{
printf (RED GREEN "%s", words);
}

This isn't so bad but I have to do it for all 8 colors
For background & text plus the special(bold, underline, strike, blink, etc)
So all in all it would take up a lot more time and space
About 300 lines id guess
I know there is a way to do this but how?
Last edited on
Use strcat()?
Does it require iostream?
No, strcat() is a standard C function for joining two C-strings. It is available under the <string.h> header.
I don't have string.h...
Try compiling the example here:
http://www.cplusplus.com/reference/cstring/strcat/
Oh do you know if I can use variables in
#define???
When you use symbols you defined, the preprocessor simply replaces the symbol with whatever you specified.
1
2
3
4
5
6
7
8
9
#include <stdio.h>
#define NUM 1;
int main(){
     printf("%d", NUM);
}
//would become
int main(){
     printf("%d", 1);
}
It works!!! Maybe iostream.h works...
I wish iostream did
But so how do I use the strcat? To make multiple #define in one statement
And the example you gave I mean can I do
Int variable=10
#define TEN "ten"???
Simply have a buffer to hold the message you are about to display. Then have your if conditions (one for each color). With each condition that matches, simply combine the color code with the buffer using strcat().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <string.h>

#define RED "\033[7;32m"
#define GREEN "\033[0;33m"
void funky(int bg_color, int text_color, int spec, char* message){
   const int max_size = 200;
   char formatted_msg[max_size];
   formatted_msg[0] = '\0';
      //Since you are using ints, you should also consider using switch statements
   if(bg_color == red)
      strcat(formatted_msg, RED);
   if(text_color == green)
      strcat(formatted_msg, GREEN);
   strcat(formatted_msg, message);
  puts(formatted_msg); 
}
Last edited on
Thank you I think it will work, but I've never learned about buffers...
Where can I learn about them
Buffers in this case are just character arrays. Think of buffers as temporary storage.
http://www.programmersheaven.com/mb/windows/109574/109574/what-is-a-buffer/
http://en.wikipedia.org/wiki/Buffer_(programming)
Thanks ...
Why does it work for these to be in "" quotes
I thought these couldn't be
Can you be more specific?

Why does what work in quotes?
Definitions you add it to an array...
So its containe in quotes ??
I am guessing you are asking why I can join RED and GREEN with the array? As I have said before, the preprocessor replaces your symbols with their definitions, so:
1
2
3
4
5
6
7
8
9
#define RED "red"

//...

strcat(formatted_msg, RED);

// Would become

strcat(formatted_msg, "red");
I know that but like I thought it didn't work in quotes just like variables
The string literal will result in a pointer, so it is as if you had passed a pointer to the function.
The code you wrote gives an error...
It says formatted_msg was called as 200 & 0
Pages: 12