Thread: Help! Defining a Function

  1. #1
    Registered User
    Join Date
    Apr 2008

    Help! Defining a Function


    What I had to do in the code is to define the function seperateWords so that it separates whole sentences/phrases. For instance "I need help" would come out as "I","need","help".

    Could anyone please find the faults in the following solution?

    #include <iostream>
    using namespace std;
        int main (void) {
        vector<string> lines, words;
        lines.push_back("Darth Archos");
        lines.push_back("is the");
        lines.push_back("Lord of the Sith");
        words = seperateWords(lines);
        cout << "The seperated words are:" << endl;
        for (unsigned i = 0; i < words.size(); i++) {
            cout << words[i] << ", ";
        cout << endl;
        return 0;
    vector <string> seperateWords (vector<string> lines) {
        vector<string > words;
        //Separating stuff goes here
    	string toFind = " "; 			//find whitespace
    	string replaceWith = "\n"; 		//replace with new line
    	for(unsigned i=0;i < lines.size(); i++) //loop through first string
    	<< lines[i] 				//read from first string
    	words.push_back('str[i]')		//add to string "words"
    	for(unsigned x=0;x < lines.size(); x++)	//loop through second string
    	<< lines[x]				//read from second string
    	words.push_back(str[x])			//add to string "words"
    	for(unsigned y=0;y < lines.size(); y++)	//loop through third string
    	<< lines[y]				//read from third string
    	words.push_back(str[y])			//add to string "words"
    	return words;

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Yup, you need to put
    vector<string> seperateWords (vector<string>);
    above main().
    I just want to use my computer in a productive manner, not learn how to use it. Elysia
    A year spent in artificial intelligence is enough to make one believe in God. Alan Jay Perlis

  3. #3
    The larch
    Join Date
    May 2006
    Well, this ain't C++

    What are you trying to do: take a string ("I need help") and break it into three strings or do you want to keep it as one string and simply replace spaces with newlines, so it becomes "I\nneed\nhelp"?

    If it is the second, then this algorithm might help: replace_copy

    For example, replacing lower-case a's with upper-case A's:
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <iterator>
    using namespace std;
    int main()
        string s1("I have a dream");
        string s2;
        replace_copy(s1.begin(), s1.end(), back_inserter(s2), 'a', 'A');
        cout << s1 << '\n' << s2 << '\n';
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
Website Security Test