Funtion receiving/returning a string value..

D

derrick

But its not working for me.

here is my code... and I know it is something so basic that I will be
embarrassed when I am corrected.. Thanks for your patience. It compiles ok,
and I can give it a string to parse, but when I execute parse it returns
nothing... Any help is greatly appreciated.



# include <string>
# include <iostream>
using namespace std;
std::string::size_type start=0, end=0;
std::string s;
std::string wordlist;


string parse(string)
{
start = s.find_first_not_of(" .,;!?");
end = s.find_first_of(" .,;!?", start);
string wordlist( s, start, end - start );
s.erase (start, end - start);
return(wordlist);
}

int main()
{
getline(cin, s);
parse(s);
cout << wordlist;
getline(cin, s);
}
 
M

Mike Wahler

derrick said:
But its not working for me.

here is my code... and I know it is something so basic that I will be
embarrassed when I am corrected.. Thanks for your patience. It compiles ok,
and I can give it a string to parse, but when I execute parse it returns
nothing... Any help is greatly appreciated.



# include <string>
# include <iostream>
using namespace std;
std::string::size_type start=0, end=0;
std::string s;
std::string wordlist;


string parse(string)
{
start = s.find_first_not_of(" .,;!?");
end = s.find_first_of(" .,;!?", start);
string wordlist( s, start, end - start );
s.erase (start, end - start);
return(wordlist);
}

int main()
{
getline(cin, s);
parse(s);

Change to:

wordlist = parse(s);
cout << wordlist;
getline(cin, s);
}

Alternatively, you can omit your 'intermediate' object and write:

cout << parse(s);

Also note that your file scope object 'wordlist' and your
function scope object 'wordlist' are two separate, unrelated
objects.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top