Thread: changing colors

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    changing colors

    Why isn't this code working properly?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    
    {
    	int a;
    	int b;
    
    	for ( ; ; )
    	{
    
    	cout << "Enter two numbers: "
    		 << "one for a background and one for text" << endl;
    
    	cin >> a;
    	cin >> b;
    
    	system ("color ab");
    
    	}
    
    	return 0;
    
    }

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Because you're sending "ab" instead of the values in a and b.
    Try:
    Code:
    #include <sstream>
    ...
    stringstream ss;
    ss << "system " << a << b;
    system(ss.str().c_str());

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Well, what do you want to do? If you want a and b to show up in the string as numbers instead of ab, you need to use strcpy() and strcat() to concatenate them into a string, then running it with system(). You can't use variables in a string like that.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Off topic:
    Are you no longer required to use "ends" when using stringstream (as opposed to strstream)? It makes sense since it gets coverted to a string first which then returns the char*.

    On topic:
    Read the FAQ for better ways of color'n your text.

    Plus, the color command requires hex digits 0-F. Type in color /? for more info.

    gg
    Last edited by Codeplug; 03-30-2003 at 03:15 PM.

  5. #5
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Code:
    #include <sstream>
    ...
    stringstream ss;
    ss << "system " << a << b;
    system(ss.str().c_str());
    Isn't there an easier way than doing that? I don't want to use <sstream>.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    73

    Unhappy no luck here...

    looks like your going to have to do it with sstream... I tried playing around with the coding using the "system("color");" command and I couldn't figure out how to throw a variable into it...

    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    
    {
    	char a[2];
    	char b[1];
    
      
    	cout << "Enter two numbers: "
    		 << "one for a background and one for text" << endl;
    
    	cin.getline(a, 2, '\n');
    	cin.getline(b, 1, '\n');
    
                     strcat(a, b); 
      
    	system("color 4B"); //Unable to enter variables into this line of code... 
              //I just threw a hex number in to display how it works w/o a variable.
    
      system("PAUSE");
    	return 0;
    
    }
    I tried playing with using the strcat and strcpy, also without any results. Now you got me curious also on how to input variables into system(const *char) lines.

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by volk
    Code:
    #include <sstream>
    ...
    stringstream ss;
    ss << "system " << a << b;
    system(ss.str().c_str());
    Isn't there an easier way than doing that? I don't want to use <sstream>.
    Yeesh, how much easier can it get?
    Don't want to use stringstream? Why not? You can easily convert digits to hex using a stringstream as well.

  8. #8
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    i dont know if this would work or not, but if you want to take the time to right it out you could do a switch
    Code:
    switch(a)
    {
      case 1:
      switch(b)
      {
        case 1:
        //whatever color here  
        break;
        case 2:
        //whatever color here  
        break;
        case 3:
        //whatever color here  
        break;
        //all the way to F
      }
      case 2:
      switch(b)
      {
         //and so on....
    but that would be awfully tedious
    but the good news is you wont have to use sstream
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Geo-Fry
    i dont know if this would work or not, but if you want to take the time to right it out you could do a switch
    Code:
    switch(a)
    {
      case 1:
      switch(b)
      {
        case 1:
        //whatever color here  
        break;
        case 2:
        //whatever color here  
        break;
        case 3:
        //whatever color here  
        break;
        //all the way to F
      }
      case 2:
      switch(b)
      {
         //and so on....
    but that would be awfully tedious
    but the good news is you wont have to use sstream
    *Gasp* you won't have to use sstream!? That IS good news!

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    *smacks head* I can't believe it was that easy! lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why I only get 8 colors out of ncurses?
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 05-08-2007, 06:06 PM
  2. Changing Text Colors Or Look In C++
    By mfm1983 in forum C++ Programming
    Replies: 3
    Last Post: 12-16-2006, 06:17 PM
  3. Changing the colors in a command line program?
    By Scott B. in forum C++ Programming
    Replies: 1
    Last Post: 08-16-2005, 10:15 PM
  4. Changing button colors?
    By V.G in forum Windows Programming
    Replies: 2
    Last Post: 10-04-2004, 10:44 PM