I am stuck in way with the for loop at the end. The whole idea is to count up to gscreenwidth and then resize the string to make it cut off at 24. We want it to skip the &w or &W anything with an & and 1 character so that it shows the color codes. Let me remind you that this is just a basic program for a much bigger one. The &w or &c etc are part of the bigger program. Also the printf and pad right is also part of the bigger program this will all be inside a frame of characters. The last two cout are just me trying to figure out the integer numbers being output.

#include <iostream>
#include <string>

using namespace std;

typedef string::size_type size_type;
typedef unsigned int u_int;
size_type gScreenWidth = 24;


/**
 * Count the number of color codes in the specified string.
 * @param s string containing text with mud color codes
 * @return length of the actual text characters in the string
 */
size_type ccStringLen( string const &s )
{
    u_int colors_count = 0;
    for( std::string::const_iterator si = s.begin(); si < s.end(); ++si )
        if( '&' == *si )
            ++colors_count;

    return s.size() - colors_count * 2;
}


string ccCenter(string const &str)
{

    int const screen_len = 80;
    int in_len = ccStringLen(str);

    int pad_left  = (screen_len - in_len)/2;
    int pad_right = screen_len - pad_left - in_len;
 
    return string( pad_left, ' ' ) + str + string( pad_right, ' ' );
    
}


int main (void)
{
    using namespace std;

    //                         1           2
    //                12345678901  23456789012345
    string title = "&wDummy Title&G and some desc&W";

    size_type title_len = ccStringLen( title );
    cout << "Title: " << title << "\nTitle Len:" << title_len<< "\n";

	int real_len = 0;
    // resize the string to fit inside the screen
    if( title_len > gScreenWidth )
    {
        
        for( std::string::const_iterator si = title.begin(); si < title.end() || title_len == 0; ++si, ++real_len)
        {
            // skip color codes
            if( '&' == *si )
                ++si;
			else
			 --title_len;
        }
        
		title.resize( real_len );
        printf("%s\n", ccCenter(title).c_str() );
		cout << title_len;
		cout << real_len;
	}
    return 0;
}

Might you be doing the old 'OR when I mean AND' thing?

si < title.end() || title_len == 0

I haven't looked very closely, but...?

si != title.end() && title_len > 0

For some reason it seems like it's not skipping & and or maybe it's also counting the last &. The whole idea is to make it resize to the maximum charcters. in this case it's 24. and not count the & or charcter after &.

Should you be trying to remove the characters you don't want?

Those charcters are color codes for a bigger program. So we want to leave those. Thats why were skipping them. so it can have different colors. Also, they do not show in the title so if I go &rHi. it's be a red Hi.

I'm sorry, I don't think I follow what it is you're trying to do.

This was some play code:

#include <iostream>
#include <string>

std::string::size_type foo(const std::string &s)
{
   typedef std::string::const_iterator ci;
   std::string::size_type length = 0;
   for ( ci it = s.begin(), end = s.end(); it != end; ++it )
   {
      if ( *it == '&' )
      {
         ++it;
      }
      else
      {
#ifndef NDEBUG
         std::cout << *it;
#endif
         ++length;
      }
   }
#ifndef NDEBUG
   std::cout << '\n';
#endif
   return length;
}

int main (void)
{
   //                              1           2
   //                     12345678901  23456789012345
   std::string title = "&wDummy Title&G and some desc&W";
   std::string::size_type title_len = title.length();
   std::cout << "title = \""   << title     << "\"\n";
   std::cout << "title_len = " << title_len << "\n";
   std::string::size_type length = foo(title);
   std::cout << "length = "    << length    << "\n";
   return 0;
}

/*
title = "&wDummy Title&G and some desc&W"
title_len = 31
Dummy Title and some desc
length = 25
*/
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.