Help! Defining a Function

This is a discussion on Help! Defining a Function within the C++ Programming forums, part of the General Programming Boards category; Hello, What I had to do in the code is to define the function seperateWords so that it separates whole ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    Help! Defining a Function

    Hello,

    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?

    Code:
    Given:
    
    
    
    #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
    Posts
    1,627
    Yup, you need to put
    Code:
    vector<string> seperateWords (vector<string>);
    above main().
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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:
    Code:
    #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, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21