There are a variety of options. In this case, probably the easiest is to print spaces BEFORE elements except the first and use a flag to track the first element:
bool perfecto(int n)
{
int suma, i;
suma = 0;
bool first = true;
for (i = 1; i < n; i++)
{
if (n % i == 0)
{
suma += i;
if(!first)
{
std::cout << " ";
}
else first = false;
cout << i;
}
}
if (suma == n)
return true;
else
return false;
}
EDIT: Other popular alternatives are printing the first item and no delimiter outside the loop completely and then inside the loop you can always pre-print the item with no if-check at all. This approach wouldn't work as well with your loop though since you don't always know when the first item will print. You can also create wrapper ostream
like classes that keep track of their internal printing state and know when to put the spaces.