Well, the void is there to make a function, to give more simple code. But to do it without the function is just as easy.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, surname;
cout << "what is your name?" << endl;
cin >> name >> surname;
for (size_t i = name.length()-1; i; --i)
if ( ispunct(name[i]) )
name.erase( i );
for (size_t i = surname.length()-1; i; --i)
if ( ispunct(surname[i]) )
surname.erase( i );
cout << surname << ", " << name << endl;
}