   More C++ Primer questions
  I'm still working with the C++ Primer, and I've run into another exercise that's giving me fits."Exercise 3.10:  Write a program to strip the punctuation from a string.  The input to the program should be a string of characters including punctuation; the output should be a string in which the punctuation is removed."I get as far as the following code:#include <iostream>#include <string>#include <cctype>using std::string;using std::cin;using std::cout;using std::endl;using std::ispunct;int main(){string entry;getline(cin, entry);cout << entry << endl;for (string::size_type index = 0; index != entry.size(); ++index)if (ispunct(entry[index])); // don't know what to do here!cout << entry << endl;return 0;}But I can't figure out any operation that I can do with the index value that will acheive my goal of deleting the punctuation character.  I have the cctype functions, and the string operations listed on p.84 at my disposal.  I tried using the "-" operation, but got a run time error.  I can find the punctuation, but how do I get rid of it?!Also, how does one put the code in their posts in a tidy little box like I see some of the posters here do?   
Quote:Original post by industrialplaidAlso, how does one put the code in their posts in a tidy little box like I see some of the posters here do?Oh, woe is man, for he no longer reads FAQs! [smile]   ;  Have you been introduced to the <algorithm> header file, yet? If so, you might want to look at remove_if.If not, the code becomes more complicated: essentially, you have to design and implement the remove_if algorithm yourself. Of particular note is that removing an element from the middle of a vector array, which is essentially what std::string is, is non-trivial: the typical approach, where sequence order is important, is to perform a cascade of swaps that move the element to be removed to the end of the sequence:// The following is PSEUDOCODEfor (string::iterator index = entry.begin(); index != entry.end(); ++index){  if (<element at index is punctuation)  {    for (string::iterator t = index, string::iterator u = index + 1;    u != entry.end(); ++t, ++u)    { string::value_type temp = *t; *t = *u; *u = temp;    }  }}Missing from the above, and left as an exercise to the reader, is the housekeeping: You must keep track of how many elements are being "removed," since they are actually only moved to the end of the sequence and it must somehow be reflected that the N last elements are to be disregarded as not part of the string.   ;  Taking a quick look at chapter 3 i'm guessing that this is what the author is looking for. Note that the question doesn't require you to only use one string in the app.#include <iostream>#include <string>#include <cctype>using std::string;using std::cin;using std::cout;using std::endl;using std::ispunct;int main(){    string entry, output;    getline(cin, entry);    cout << entry << endl;    for (string::size_type index = 0; index != entry.size(); ++index) {   // if is not punct, add the character to the string to output    if ( !ispunct(entry[index]) )  output += entry[index];    }    cout << output << endl;    return 0;};  Will:  I didn't realize that I could attach the "!" symbol to a function to negate it.  That's quite a handy feature, and I will have to remember it.  Using that and your advice to copy the 'good' characters to a new string rather than trying to remove the 'bad' ones from the old, the exercise was a breeze.Oluseyi:  At this point in the Primer I haven't encountered the <algorithm> function, nor have I seen the string::iterator or string::value_type types.  I'm also unclear as to what the "*" does in your code.  I'll have to check back here after I get a little further in to review.Thanks for the help.   ;  The "*" dereferences (sp?) a pointer.   