Sunday, July 12, 2009

C++ string question?

OK! Just to let everyone know...this project has already been turned in--I just need to know what I could've done to make the program take str2 as a string of characters and erase all of them. I could not get the for loop to keep searching/ finding anything past the first occurrence. I need to know for a coming final in c++ in 2 weeks. thanks





#include%26lt;iostream%26gt;





#include%26lt;string%26gt;





using namespace std;





int main()





{





string str1, str2, ans;





do{





cout%26lt;%26lt;"Please enter a string (40 characters max)--%26gt;"%26lt;%26lt;endl;


getline(cin, str1);





cout%26lt;%26lt;"Please enter the characters to be removed(20 characters max) --%26gt;"%26lt;%26lt;endl;


getline(cin, str2);





for(int i=0; i%26lt;=40; i++);


{





string::size_type loc=str1.find(str2, 0);


(loc!=string::npos);


{


str1.erase(loc, +1);





cout%26lt;%26lt;"First string with characters removed --%26gt;"%26lt;%26lt;str1%26lt;%26lt;endl;





}


}








cout%26lt;%26lt;"Would you like to do another(y or n)?"%26lt;%26lt;endl;


getline(cin, ans);


}


while(ans[0]=='y'||ans[0]=='Y');


return 0;


}

C++ string question?
One reason your 'for' loop doesn't work is the semi-colon at the end of it:


for(int i=0; i%26lt;=40; i++);





Another problem is this statement, that looks like you wanted it to be an 'if' statement:


(loc!=string::npos);





Your compiler should have told you this statement has no effect.





Even with that, there are problems with the code. There is no need to hardcode 40 in your for loop, or prompt for a max of 40 or 20 characters. Also, with this: str1.erase(loc, +1) you'll only erase str2 if its length is 1. Also, to be more efficient, you don't need to repeatedly start your search at str1[0].





So, implementing all those suggestions, my improved version of your code looks like this:





int main(int argc, char *argv[]) {


string str1, str2, ans;


string::size_type loc = 0;





do {


cout%26lt;%26lt;"Please enter a string --%26gt; ";


getline(cin, str1);


cout%26lt;%26lt;"Please enter the characters to be removed --%26gt; ";


getline(cin, str2);


do {


if ((loc = str1.find(str2, loc)) != string::npos) {


str1.erase(loc, str2.length());


cout %26lt;%26lt; "First string with characters removed --%26gt;" %26lt;%26lt; str1 %26lt;%26lt; endl;


}


} while (loc != string::npos);


cout %26lt;%26lt; "Would you like to do another(y or n)?";


getline(cin, ans);


} while (ans[0]=='y' || ans[0]=='Y');


return 0;


}





One other thing I might do for a production version of the program would be to move the 'cout' outside of the 'for' loop, so you just display the final result. As it is, though, it's nice to watch the progress as occurrences of str2 are removed.





EDIT: it just occurred to me that str2 probably isn't a substring to be removed, but a list of characters to be removed. If so, you were missing something for this as well. Check out the code below. I think this is the solution you're looking for:





int main(int argc, char *argv[]) {


string str1, str2, ans;


string::size_type loc = 0;





do {


cout%26lt;%26lt;"Please enter a string --%26gt; ";


getline(cin, str1);


cout%26lt;%26lt;"Please enter the characters to be removed --%26gt; ";


getline(cin, str2);


if ((str1.length() %26gt; 0) %26amp;%26amp; (str2.length() %26gt; 0)) {


do {


while ((loc = str1.find(str2[0], 0)) != string::npos) {


str1.erase(loc, 1);


}


str2.erase(0,1);


} while (str2.length() %26gt; 0);


cout %26lt;%26lt; "First string with characters removed --%26gt;" %26lt;%26lt; str1 %26lt;%26lt; endl;


}


cout %26lt;%26lt; "Would you like to do another(y or n)?";


getline(cin, ans);


} while (ans[0]=='y' || ans[0]=='Y');


return 0;


}


No comments:

Post a Comment