Formating a table of integer columns 'correctly'

It took me a surprisingly long time to code even this table, needed for some debugging.

16:54:04.029 -> i   File   Year   Day  Until
16:54:04.029 -> 0   129    2023   309   123
16:54:04.029 -> 1   136    2023   311   123
16:54:04.029 -> 2   137    2023   312   123
16:54:04.029 -> 3   78    2023   315   123
16:54:04.029 -> 4   82    2023   316   123
16:54:04.029 -> 5   138    2023   321   123
16:54:04.029 -> 6   131    2023   359   123
16:54:04.029 -> 7   71    2023   360   123
16:54:04.029 -> 8   96    2023   365   123
16:54:04.029 -> 9   95    2023   1   123
16:54:04.029 -> 10   90    2023   17   123
16:54:04.029 -> 11   139    2023   40   123

But although it serves its purpose, how could I have most quickly made it look like this tidy version please?

16:54:04.029 -> i   File   Year   Day  Until
16:54:04.029 ->  0  129    2023   309   123
16:54:04.029 ->  1  136    2023   311   123
16:54:04.029 ->  2  137    2023   312   123
16:54:04.029 ->  3   78    2023   315   123
16:54:04.029 ->  4   82    2023   316   123
16:54:04.029 ->  5  138    2023   321   123
16:54:04.029 ->  6  131    2023   359   123
16:54:04.029 ->  7   71    2023   360   123
16:54:04.029 ->  8   96    2023   365   123
16:54:04.029 ->  9   95    2024     1   123
16:54:04.029 -> 10   90    2024    17   123
16:54:04.029 -> 11  139    2024    40   123

(BTW, when composing this, that did not faithfully show my intended text. So I made a screnshot instead. But after Send, it now looks OK.)

The maximum values of i, File and Day are 99, 999 and 366 respectively. All values are int types. Here's my code:

void listAllEvents()
{
  int eventArraySize = sizeof(notableEvents) / sizeof(notableEvents[0]);
  Serial.print(F("eventArraySize = "));
  Serial.println(eventArraySize);
  Serial.println(F(""));
  Serial.println(F("ALL EVENTS CURRENTLY DEFINED"));
  Serial.println(F("----------------------------"));
  Serial.println(F("i   File   Year   Day  Until"));

  for (i = 0; i <= eventArraySize-1; i++)
  {
    Serial.print(i);
    Serial.print(F("   "));
    Serial.print(notableEvents[i].fileNumber); // 'File' in table
    Serial.print(F("    "));
    Serial.print(Year);
    Serial.print(F("   "));
    Serial.print(notableEvents[i].dayOfYear);  // Year in table
    Serial.print(F("   "));
    Serial.println(F("123")); // temporary
  }
  while (1 == 1); // Stops program from proceeding

} // End listAllEvents

Not sure if it's relevant, but the source of File and Day is from a struct; here's an extract:

struct NotableEvent
{
  int fileNumber;
  int dayOfYear; //
};

NotableEvent notableEvents[] =
{
  {129, 309},
  {136, 311},
  {137, 312},
  {78, 315}, 
  {82, 316}, 
  {138, 321},
  {131, 359},
  {71, 360}, 
  {96, 365}, 
  {95, 1},
  {90, 17},
  {139, 40}, 
  etc

sprintf()

What type of Arduino or compatible are you using? Some allow Serial.printf()

1 Like
   char buf[80];
   snprintf(buf, sizeof buf, "%2d  %3d    %4d   %3d   123\n",
      i, notableEvents[i].fileNumber, Year, notableEvents[i].dayOfYear);
   Serial.print(buf);
1 Like

@PaulRB
@van_der_decken

Thanks both, that was fast. I'm using a UNO on my breadboard and a Nano for the cased version. The code is from the Nano.

My first reading on sprintf left me a bit confused about its syntax, so I postponed further study. Will shortly try that suggested code, thanks. It will speed my understanding too; I learn best from practical examples like this.

Neither Uno or Nano allow Serial.printf()

By the way, are you crazy? Nano is breadboard compatible. Uno is not. Ok, Uno is not incompatible with breadboard, somewhat like a fish is not incompatible with a bicycle. :wink:

Worked a treat, many thanks. I'll do the necessary reading later :slightly_smiling_face:

1 Like

'on' wasn't the right word. The breadboard accommodates LCD, RTC, temperature circuitry and a DFR MP3 module. And a lot of dupont to the UNO of course.

Yeah, always takes a lot of Dupont to lash the fish to the bicycle :wink:

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.