How to use find() and substr() member function

Hi, I am having trouble using find() and substr() to extract 2 names out of one string. eg.

cout << "Enter full name (last,first)";
string name;
cin >> name

cout << "First name" << [this is where i am trying to output the first name];
cout << "Last name" << [this is where i am trying to output the last name];

There may be no spaces in the string name and we are to use find() and substr() to solve this. Please help!
last name and first name are separated by comma ( , )?
i think this code of mine could help... i assume that last name and first name are separated by comma and no spaces included on names
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstring>
using namespace std;

int main ()
{
    string name,Fname,Lname;
    cout << "Enter full name (last,first): ";
    cin >> name;
    for(int i=0;i<name.size();i++)
    {
            if(name.at(i)==','){
                 Fname=name.substr(0,i);
                 Lname=name.substr(i+1);         
            }        
    }
    cout << "First name: " << Fname<<endl;
    cout << "Last name: " << Lname<<endl;
    system("pause");
}


in case that names have spaces you'll need to replace line 9 of the previous code with this one
getline(cin, name);
I'm not sure if we are allowed to use

for(int i=0;i<name.size();i++)
{
if(name.at(i)==','){
Fname=name.substr(0,i);
Lname=name.substr(i+1);
}
}


we are only to use #<iostream> and #<iomanip> as well.

Could someone explain how the find() works?

thank you~
I'm not sure why <cstring> was included in the example. I see no reason for it. As far as the string member functions go, why not just read the interface documentation? This website has a reference page for all algorithms and containers which thoroughly explain how they work and provide examples. Click the reference link in the left hand pane of the web page. I've included a couple for your convenience. I have included the link for std::find as well because it is more generic and uses iterators. I prefer using it when dealing with strings rather than std::string::find.

http://cplusplus.com/reference/string/string/
http://cplusplus.com/reference/algorithm/find/

If you have more specific questions feel free to ask those but the references above should be very helpful.
@kempofighter
<cstring> is equivalent for <string>
<cstring> is equivalent for <string>


No, cstring gives you strcmp, stristr, and random C functions. You want string, which has C++ strings in it.
Topic archived. No new replies allowed.