Question of C (Not C++) - change colour

Question of C (Not C++) - change colour

Page 1 of 1

7 Replies - 1266 Views - Last Post: 18 September 2009 - 10:08 PM Rate Topic: -----

#1 lowilliam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-September 09

Question of C (Not C++) - change colour

Posted 18 September 2009 - 07:54 AM

This is my program.
I want to print the first result in red colur , 2nd in green colour and the other in blue colour, what should i do?
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int i,j,smallest=10,printed[10];
    char character[10][10]={"Aa kjjb","aA kkllB","BbA adf","Biyb alyd","Zkzz duwe","AjcA bsk","BhjB bfd","BfgC asd","Bgfb Cw","ZfdD dfz"};
    /*To change letter to capital letter*/
    for(i=0;i<10;i++)
       {
        for(j=0;j<10;j++)
           if('Z'>=character[i][j]&&character[i][j]>='A')
              character[i][j]=character[i][j]+32;
        for(j=0;j<10;j++)
           if(' '==character[i][j])
              if('z'>=character[i][j+1]&&character[i][j+1]>='a')
                 character[i][j+1]=character[i][j+1]-32;
        character[i][0]=character[i][0]-32;
        }
    /*Cancel the value*/
    for(i=0;i<10;i++)
      printed[i]=0;
    /*Start arranging*/
    for(i=0;i<10;i++)
       {
        for(j=0;j<10;j++)
           if(strcmp(character[j],character[smallest]) == -1 && printed[j]==0)
              smallest=j;
           printf("   %-3d : %-10s\n",i+1,character[smallest]);
        printed[smallest]=1;
        smallest=10;
        }
  system("PAUSE");	
  return 0;
}

the Output i want:
1 : Aa Kjjb
2 : Aa Kkllb
3 : Ajca Bsk
...
Could you please give me the correct answer and more detail (with example) of changing the colour?

*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 18 September 2009 - 08:21 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Question of C (Not C++) - change colour

#2 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Question of C (Not C++) - change colour

Posted 18 September 2009 - 08:06 AM

Make a GUI program.

You cannot change color as long as you have your output on a console window.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Question of C (Not C++) - change colour

Posted 18 September 2009 - 08:23 AM

How this is done is dependent on the system, and on what graphics libraries are available. There are some tutorials and snippets on this site -- many of which were written by a user named Hyper -- which demonstrate color in the console. Tutorials are available here, snippets here.
Was This Post Helpful? 0
  • +
  • -

#4 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Question of C (Not C++) - change colour

Posted 18 September 2009 - 08:40 AM

oops sorry

you can change color

here's an example


#include <stdio.h> 
#include <windows.h> 

int main ( void )
{
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  WORD wOldColorAttrs;
  CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
  
 //  save the current color information
   
  GetConsoleScreenBufferInfo(h, &csbiInfo);
  wOldColorAttrs = csbiInfo.wAttributes; 
  

   //Set the new color information

  SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
  
  printf ( "This is a test\n" );


   //Restore the original colors
   
  SetConsoleTextAttribute ( h, wOldColorAttrs);
  system("pause"); 
				  
  return 0;
}



Was This Post Helpful? 0
  • +
  • -

#5 jeff666  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 193
  • Joined: 30-December 08

Re: Question of C (Not C++) - change colour

Posted 18 September 2009 - 09:19 AM

See the well-known console.c Microsoft sample showing all Win32 Console apis
It's the best and complete sample for Console apis.

This post has been edited by jeff666: 18 September 2009 - 09:20 AM

Was This Post Helpful? 0
  • +
  • -

#6 lowilliam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-September 09

Re: Question of C (Not C++) - change colour

Posted 18 September 2009 - 09:49 PM

View Postsnowflakes, on 18 Sep, 2009 - 07:40 AM, said:

oops sorry

you can change color

here's an example


#include <stdio.h> 
#include <windows.h> 

int main ( void )
{
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  WORD wOldColorAttrs;
  CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
  
 //  save the current color information
   
  GetConsoleScreenBufferInfo(h, &csbiInfo);
  wOldColorAttrs = csbiInfo.wAttributes; 
  

   //Set the new color information

  SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
  
  printf ( "This is a test\n" );


   //Restore the original colors
   
  SetConsoleTextAttribute ( h, wOldColorAttrs);
  system("pause"); 
				  
  return 0;
}




how about other colour?
green blue etc
And thanks for help
Was This Post Helpful? 0
  • +
  • -

#7 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Question of C (Not C++) - change colour

Posted 18 September 2009 - 10:00 PM

Did you try replacing FOREGROUND_RED with FOREGROUND_BLUE? ;)
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Question of C (Not C++) - change colour

Posted 18 September 2009 - 10:08 PM

if you are programming on windows than you will find a number of useful functions in my windows console snippet.

it features a number of useful functions suchas:

SetColor() -- sets the foreground color
SetColorAndBackground() -- sets background color
gotoXY() -- locates the cursor to begin printing where you want
ClearConsole() -- clears the screen
ClearConsoleToColors() -- will set the colors AND clear the screen
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1