Well, theres a few ways you can do this, one method which I would probably use would be to read in the names, and manually remove all punctuation. To do that, you simply loop through each character, check if it is punctuation, if so, then erase it like this:
#include <iostream>
#include <string>
using namespace std;
void removePunct(string &str) {
for (size_t i = str.length() - 1; i; --i)
if ( ispunct(str[i]) )
str.erase( i );
}
int main()
{
string name, surname;
cout << "what is your name?" << endl;
cin >> name >> surname;
removePunct( name );
removePunct( surname );
cout << surname << ", " << name << endl;
}
Hope this helps.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
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;
}
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129