Thread: New to C++
View Single Post
Old 10-26-2005, 06:54 PM   #12 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
Redhead, I can't seem to figure out what I have to do with parsing out the word. I'm pretty sure it has something to do with:

Code:
char_str = (char*) malloc(in_str.size()*sizeof(char));
Is char_str the size of the word you will be removing in the phrase, and you are reserving room in memory to hold the word, for which you check against all words in that phrase through a loop until you find it? I think it's something like that but I can't figure out, I'll post what I have. The areas I didn't understand are commented

Code:
/*
Make code that removes the first encountered substring from a C-style string.
I want you to explain every line of code. You need to prove you'understand char* handling.

Like this:
char* theString = "Life is hard, then you die.";
remove_substring(theString, "hard");
//theString = "Life is, then you die."
*/
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int remove_string(char* in_str, string remove_str);

int main()
{
    string in_str;
    string remove_str;
    char* char_str; //i'm going to have to use the pointer address and length of string to remove it from the in_str?
    cout << "Input string to process: ";
    cout.flush();
    getline(cin, in_str); //why not just cin? doesn't cin accept spaces if it processes a string?
    cout << "Input word to be removed: ";
    cin >> remove_str;
    char_str = (char*) malloc(in_str.size()*sizeof(char)); //not sure what this does?
    if(!char_str) //error checking for a non-string inputsystem("pause");
    {
        cout << "Error allocating space for input string." << endl;
		system("pause");
        return -1;
    }
    if(!memcpy(char_str, in_str.c_str(), in_str.size())) //an anomoly case
    {
        cout << "Error copying string to char array." << endl;
		system("pause");
        return -1;
    }
    system("pause");
    if(!remove_string(char_str, remove_str)) //if both arguments aren't valid (remove_str doesn't match anything in in_str)
    {
        cout << "Unable to remove the string" << endl;
		system("pause");
        return -1;
    }
//    in_str(char_str); //Not sure what this is all about...? I want to process the remove string here don't I?
      remove_string(char_str, remove_str);
    cout << "After eliminating \"" << remove_str <<"\" the string is: " << in_str << endl;
	system("pause");
    return 0;
}

    /* this is where your come in,
     * make the remove_string() function
     * so it will remove the string provided 
     * in remove_str from the char array
     * provided in in_str.
     * It should return 1 on success and 0 on error
     */

int remove_string(char* in_str, string remove_str)
{
	cout << sizeof(in_str);
	return 0;
}
Deliverance is offline   Reply With Quote