Thread: New to C++
View Single Post
Old 10-27-2005, 04:30 AM   #13 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Code:
    char* char_str; //i'm going to have to use the pointer address and length of string to remove it from the in_str?
this is just declaring that char_str is a char* normaly what would be holding a string.
Code:
   getline(cin, in_str); //why not just cin? doesn't cin accept spaces if it processes a string?
This is to ensure you get the entire line as written from the user, when you use cin >> std::string you can't be sure to get everything (space/tabs/etc) and you might end up with some garbage left in your instream.
Code:
 char_str = (char*) malloc(in_str.size()*sizeof(char)); //not sure what this does?
Reserve the needed size of memory in order to have your char_str hold the in_str without creating buffer overrun or any illformed effect.
Could have been achieved with
Code:
char_str = new char(in_str.size());
aswell.
Code:
    if(!char_str) //error checking for a non-string inputsystem("pause");
If the memory allocation fails, the char_str will be NULL, thus we can break off the operation at this point, since we're never gonna have expected behavior when you can't control your memory heap/stack.
Code:
   if(!memcpy(char_str, in_str.c_str(), in_str.size())) //an anomoly case
Copy the user inputted string into our char_str which we at this point know will have memory enough to hold it, this could be achieved with
Code:
strcpy(char_str, in_str.substr(0));
aswell.
Code:
  if(!remove_string(char_str, remove_str)) //if both arguments aren't valid (remove_str doesn't match anything in in_str)
True, so theres no need to go further, the intended word that should be removed, can't bee, if it's not pressented in the string.
Code:
//    in_str(char_str); //Not sure what this is all about...? I want to process the remove string here don't I?
Reassign the char_str to in_str using the string( const char* str ); in order to let in_str hold the string with the intended word removed. So NO you don't want to process the remove string, since you've already done that.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote