'C++ - Is there a way to print out a comma if only when there's another output printed after the comma itself? [duplicate]

I'm trying to write a piece of code that will only print a comma after an output if there is another output printed out after it. I use

  for(int j = 0; j < size; j++)
       {
          if(price[i][j] > 0)
          {
             cout << airports[j] << ", ";
          }
                  
       }

The problem is I don't know how to get rid of the last comma that is added with the last entry in the list. It outputs this:

Airports' destinations: 

BUR (3): LAX, SFO, SMO, 
LAX (1): BUR, 
SFO (2): BUR, SMO, 
SMO (2): BUR, SFO, 

How do I remove the comma at the end of every output?



Solution 1:[1]

for(int j = 0, comma = false; j < size; j++)
{
    if (price[i][j] > 0)
    {
        if (comma)
            cout << ", ";
        cout << airports[j];
        comma = true;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1