I have two code snippets basically I want a program to do color in the console but I only want it to run a certain function based on the compiler (I will use windows.h but my teacher will use conio.h because she uses Borland C++ 5.02 ) I have a idea how to do this but I'm not sure...

#include <windows.h>   // WinApi header
f o r  example:
if ( compiler is Borland C++ )
color = conColor
else
color = winColor
/* I know you can't assign like just but I want you to have an idea of how I want to impement it thank you in advance... */
/* Not my windows api sniplet I think I found it here or somewhere on the internet ...  */
winColor()
{
  HANDLE  hConsole;
	int k;
	
  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

  // you can loop k higher to see more color choices
  for(k = 1; k < 255; k++)
  {
    SetConsoleTextAttribute(hConsole, k);
    printf("%3d  %s\n", k, "I want to be nice today!");
  }
  SetConsoleTextAttribute(hConsole, 1);
  printf ("this is ");
  SetConsoleTextAttribute(hConsole, 2);
  printf ("in a different printf \n");

  getchar();  // wait
  return 0;
}

and

#include <conio.h>
#include <stdio.h>

conColor()
{
  printf("Default Color.\n");
  textcolor(YELLOW);
  cprintf("YELLOW");
  printf("\n");
  textcolor (BLUE);


          cprintf ("       *||*");
          printf  ("\n");
          cprintf ("     ** || **");
          printf  ("\n");
          cprintf ("   **        **");
          printf  ("\n");
          cprintf ("     **        **");
          printf  ("\n");
          cprintf ("**        **");
          printf  ("\n");
          cprintf ("**        **");
          printf  ("\n");
          cprintf ("**        **");
          printf  ("\n");
          cprintf ("**        **");
          printf  ("\n");
          cprintf ("**    **");
          printf  ("\n");
          cprintf ("****");
          printf  ("\n");

  getch ();
}

These are not the functions I will use but basically a function that will accept the string then 'change' the color then change the color back...(dont' need help on that just choosing the correct function for the compiler ) ...

thanks I can't find anything that borland defines that MINGW32 does so as both systems use windows I will use the windows one...

What I would do is define a macro for my compiler, then it it's not defined the compiler would be the one your teacher uses. For example if you are using vc++ 2008 then you could use the predefined macro _MSC_VER which is the compiler version

#ifdefined (_MSC_VER)
#include <windows.h>
#else
#include <conio.h>
#endif
// blabla

thanks I can't find anything that borland defines that MINGW32 does so as both systems use windows I will use the windows one...

You could always create your own macro, such as __MINGW__ then use it like I showed in my previous post.

Thank you so much Ancient Dragon that really helped me ... it helped to solve my problem... (now I can work on the code for the specifics)

// change text color in Windows console mode
// colors are 0=black 1=blue 2=green and so on to 15=white
// colorattribute = foreground + background * 16
// to get red text on yellow use 4 + 14*16 = 228
// light red on yellow would be 12 + 14*16 = 236
// tested with Pelles C  (vegaseat)
#include <conio.h>
#if!defined (__CONIO_H)
#include <windows.h>
#define BOR 0
#else

# define BOR 1
#endif
// blabla
#include <stdio.h>
//#include <windows.h>   // WinApi header

int main()
{
    if (BOR == 1) {
        printf ("Compiler is borland\n");
    }
    else if (BOR == 0){
        printf ("Compiler is not borland\n");
    }
    getchar ();

 /* HANDLE  hConsole;
	int k;

  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

  // you can loop k higher to see more color choices
  for(k = 1; k < 255; k++)
  {
    SetConsoleTextAttribute(hConsole, k);
    printf("%3d  %s\n", k, "I want to be nice today!");
  }
  SetConsoleTextAttribute(hConsole, 1);
  printf ("this is ");
  SetConsoleTextAttribute(hConsole, 2);
  printf ("in a different printf \n");

  getchar();  // wait
  return 0;
}
void ColorPrint (const int ColTxtnum,const char *text) {

    HANDLE  hConsole;
    int k;

    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, ColTxtnum); /* Place it on screen
    SetConsoleTextAttribute(hConsole, 1); /* Change back to normal */

}

But one more question how do I define functions in the precursor....

how would i define it so that for example if the compiler was codeblocks (mig) it would call ColorPrintWin (non borland compiler) function?
eg.

#include <conio.h>
#if!defined (__CONIO_H)
#include <windows.h>
   void ColorPrint (const int ColTxtnum,const char *text) {

    HANDLE  hConsole;
    int k;

    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, ColTxtnum); /* Place it on screen*/
    SetConsoleTextAttribute(hConsole, 1); /* Change back to normal */

} /** I'm guessing I can't do that.... **/

#define BOR 0
#else

# define BOR 1
#endif
// blabla
#include <stdio.h>
//#include <windows.h>   // WinApi heade

EDIT: Dave Should I use the version defined for borland instead?

why don't you just use the same compiler your teacher is using? It would solve all your problems.

Borland C++ 5.02 is too Old and if you are working though a lot of code there is no way you can stroll though function by function . . . anyway thanks I think i can do what I posted in the above post ...

A better question is what of any use are you going to learn from this teacher stuck in the stone age?

You already seem to know more than they do.

All those ifdef's are messy!!!

// screen.h
void ColorPrint (const int ColTxtnum,const char *text);

// screen_borland.c
#include <conio.h>
#include "screen.h"
void ColorPrint (const int ColTxtnum,const char *text) {
}


// screen_win.c
#include <windows.h>
#include "screen.h"
void ColorPrint (const int ColTxtnum,const char *text) {
}

// main.c
#include <stdio.h>
#include "screen.h"

int main ( ) {
  ColorPrint(RED_COL,"hello world");
  return 0;
}

All the configuration happens in the project.
Both projects are essentially identical, except for the appropriate choice of screen_borland.c or screen_win.c

And if you wanted to add screen_linux.c in future, you can see how easy that could be :)

And not a single #if anywhere in the portable code - woo hoo!

commented: Big picture stuff --more attention than I gave this. +13

oh, i get what your doing that will help me in the future thanks...
so basically "screen.h" would contain all the different codes that I need?!
I'm not getting the // screen_win.c and // screen_borland.c and
// screen.h could you explain that please ... how does the compiler know which one to use?

>>I'm not getting the // screen_win.c and // screen_borland.c

You will have two *.c files. screen_win.c contains the code for your compiler, and screen_borland.c will contain the code that you turn in to your instructor. Regardless of how you do it you wil have to compile your program with that borland compiler so that your instructor will not have any problems compiling it.

> so basically "screen.h" would contain all the different codes that I need?
No, screen.h is the INTERFACE.

Just like stdio.h is an INTERFACE to the standard C library. What you see (from an application point of view) remains the same. Every compiler has a different implementation of LibC, but that detail is hidden from you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.