Printing variable length Strings

Hi All, I have been programming Pascal for many many years and struggling with C++ a bit. In the following code, I know the max length lenBlanks will never be more than 5, so the 10-blanks will never be exceeded. I found strcpy( and strncpy( but can't seem to get it to do what I need. Do I have to first set up a string with the 10-blanks or what?

void lcdPrintInt(byte aCol, byte aRow, int anInt, byte lenBlanks)
{
  if (blanks > 0) // I need to clear the old longer data
  { lcd.setCursor(aCol,aRow);
// In Pascal I would do this...
// lcd.print(Copy('          ',1,lenBlanks));  // Start at position 1 and copy lenBlanks
  }
  lcd.setCursor(aCol,aRow);
  lcd.print(anInt);
}

Thanks

This can be as simple as one sprintf call to fill a buffer with your number, justified left or right as you choose, and padded with spaces or zeroes.

I'm not exactly sure what you want it to look like at the end, but the answer you seek lies in the google results for "C++ sprintf"

Most of the C string function follow a parameter sequence of "destination, source". So, for string copy, it is:

strcpy(char *destination, const char *source);

The const keyword prevents the function from messing around with the source of the copy. strncpy() is much the same, but adds a third argument to specify the number of characters to copy from source to destination:

strncpy(char *destination, const char *source, size_t charsToCopy);

Many LCD displays (and their libraries) require that numeric variables be converted to a string format before displaying them. If yours is like that, you need to add a few lines:

void lcdPrintInt(byte aCol, byte aRow, int anInt, byte lenBlanks)
{
   char buffer[10];

   memset(buffer, ' ', sizeof(buffer) );        // Set all elements to space
   lcd.setCursor(0, aRow);                      // Set cursor to start of the row
   lcd.print(buffer);                           // Clear the row

   itoa(anInt, buffer, 10);                     // Convert the int to a base 10 string of the value
   lcd.setCursor(aCol,aRow);                 
   lcd.print(buffer);
}

Personally, I'd get rid of the fourth argument to the function and just clear the entire row. (Clearly, this won't work if you need to keep part of the row.) You may need to increase the size of buffer to correspond to the display size.

econjack: Many LCD displays (and their libraries) require that numeric variables be converted to a string format before displaying them. If yours is like that, you need to add a few lines:

Wow, thanks econhjack. VERY much appreciated. I had no idea it required so much work and you can guess why I was making such a mess of it thinking along Pascal lines.

The LCD library I musing seems happy with Int, Float and String so will play with your examples until I understand it.

{edit} I forgot to address the clear-row thing. I need to clear two small patches, one 4-bytes the other 5-bytes so I cannot clear the entire row. Where do I use the lenBlanks? I have just been messing with it and it is interesting but not yet working to plan. :D {/edit}

It is good to get some actual help rather than worthless pointers on what to search for. I never ask here until I am stumped after a lot of searches.

For now, I have gone to a brute force method. :)

  switch(lenBlanks)
  { case 4: lcd.print("    ");
     break;
     case 5 : lcd.print("     ");
     break;
  }

Delta_G: This can be as simple as one sprintf call to fill a buffer with your number, justified left or right as you choose, and padded with spaces or zeroes.

Trite enough. But no help. I had done about an hour of searching and testing stuff and could not make it work. Given the actual way to do it by econjack, I can see why I was not making any headway.

You do realize this is a "help" forum?

WaitSome: Trite enough. But no help. I had done about an hour of searching and testing stuff and could not make it work. Given the actual way to do it by econjack, I can see why I was not making any headway.

You do realize this is a "help" forum?

Yes, and I'm sorry if my answer wasn't helpful enough for you. Maybe you should go and ask for your money back.

In all fairness to Delta_G, the sprintf() function has a lot of features you may find useful...too many to explain in a single post here. Take a look at its documentation as it is a very versatile function. That said, I don't use it too often because it is often an h-bomb-to-kill-an-ant...you rarely use more than 10% of its functionality, so it ends up chewing a lot of your memory unnecessarily. However, it is reliable, full featured, and worth your investigation.

It's not trite. He's right. sprintf() can perform padding with alignment.

It only took me a few minutes to find this:

Padding with spaces

This is more or less the same as leading zeros, except it uses leading (or, if told, trailing) spaces to complete the field width. This is useful for lining up multiple lines of data into a report, for instance (though in that case, you may also want to specify a maximum field width to truncate long values - more on that below). The syntax is just like leading zeros, but drop the leading zero:

printf("%6s", 'foo'); # prints ' foo'

By default, leading spaces are used, so values appear to be right-aligned in their field. To reverse this, put a '-' sign before the field width:

printf("%-6s", 'foo'); # prints 'foo '

... and I didn't know that...