Thread: using substr

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    21

    using substr

    Can someone look at this program and tell me why i am getting the error that
    **c:\documents and settings\burley\desktop\carrie\cpp1.cpp(26) : error C2664: 'substr' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'unsigned int'
    here is the program!!!!
    #include <iostream>
    #include <string>

    using namespace std;
    int main ()
    {
    string fullname;

    cout<< "Please enter you first and last name:";

    cin>>fullname;

    getline(cin,fullname,'\n');

    string spaceposition;

    spaceposition= fullname.find(" ",0);

    string fname;

    fname=fullname.substr(0,spaceposition);

    string lname;

    lname=fullname.substr(spaceposition,fname.size());

    cout << "First Name: "<<fname<< endl;
    cout << "Last Name: " <<lname<< endl;

    return 0;
    } ///Thank you !!!!!

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You don't need cin>>fullname and getline(cin,fullname). Try -

    Code:
    #include <iostream> 
    #include <string> 
    
    using namespace std; 
    int main () 
    { 
    string fullname; 
    
    cout<< "Please enter you first and last name:"; 
    
    getline(cin,fullname,'\n'); 
    
    int spaceposition; 
    
    spaceposition= fullname.find(" ",0); 
    
    string fname; 
    
    fname=fullname.substr(0,spaceposition); 
    
    string lname; 
    
    lname=fullname.substr(spaceposition,fullname.size()); 
    
    cout << "First Name: "<<fname<< endl; 
    cout << "Last Name: " <<lname<< endl; 
    
    return 0; 
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    21

    Talking using substr

    Thank you very much I appreciate your help!!!!!!!
    Works great!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. substr problems
    By spudval in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2007, 05:48 AM
  2. Substr
    By mhenderson in forum C Programming
    Replies: 4
    Last Post: 08-04-2006, 01:44 AM
  3. something's up with my substr() test
    By indigo0086 in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2006, 06:50 AM
  4. char * substr
    By dfom_2004 in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2006, 07:25 PM
  5. substr question
    By gustavosserra in forum C++ Programming
    Replies: 5
    Last Post: 08-30-2003, 07:44 PM