Need to get last 2 characters from string

I need to get the last 2 characters from the last name which is input from the keyboard. I am trying to use "thestring.copy(str, x, n). I do not know the length of the name being input. my attempt at the function is
last.copy(str, strlen -2, 2)
I am using strlen - 2 to find the end of the string and copy from the last 2 characters. I do not know if I am on the right path and also I am getting an error
 Rev 2.cpp||In function 'int main()':|
 Rev 2.cpp|39|error: cannot convert 'std::string' to 'const char*' for argument '1' to 'size_t strlen(const char*)'|
 Rev 2.cpp|54|warning: pointer to a function used in arithmetic|
 Rev 2.cpp|54|error: invalid conversion from 'size_t (*)(const char*)' to 'unsigned int'|
 Rev 2.cpp|54|error:   initializing argument 2 of 'typename std::basic_string<_CharT, _Traits, _Alloc>::size_type std::basic_string<_CharT, _Traits, _Alloc>::copy(_CharT*, typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type) const [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'|
 Rev 2.cpp|58|error: expected ';' before '}' token|
||=== Build finished: 4 errors, 1 warnings ===|


Here is the code

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;


//char first [20];
//char last [20];
char str [20];
string first;
string last;
int firstlen;
int lastlen;
char nfirst [4];
char nlast [20];
char lastone;
char lasttwo;
int main()
{
    //string first, nfirst;

    cout << "== Star Wars Name Generator ==" << endl;
    cout << "Enter first name" << endl;
    cin >> first;
    //cin.getline (first, 20);
    //getline (cin, first);
    //firstlen = strlen (first);
    if (first.length() < 3)
    {
        cout << "First name must be at least three letters" << endl;
    }
    else
    {

        cout << "Enter last name" << endl;
        cin >> str;
        lastlen = strlen (last);
        //cin.getline (last, 20);
        //getline (cin, last);

        //lastlen = strlen (last);

       if (last.length() < 2)
        {
            cout << "Last name must be at least two letters" << endl;
        }
        else

        {

            first.resize (3);
            last.copy(str, strlen -2, 2)
            //last.resize (2);


        }
    }

    cout << first << " " << last << str << endl; //used to check array variables. not part of code
    cout << "Your Star Wars name is: " << first << "-" << last << endl;


}
I figured out how to get the last 2 characters but now need to capitalize just the first letter of the 2. Thanks

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;


//char first [20];
//char last [20];
char str [20];
string first;
string last;
int firstlen;
int lastlen;
char nfirst [4];
char nlast [20];
char lastone;
char lasttwo;
int main()
{
    //string first, nfirst;

    cout << "== Star Wars Name Generator ==" << endl;
    cout << "Enter first name" << endl;
    cin >> first;
    //cin.getline (first, 20);
    //getline (cin, first);
    //firstlen = strlen (first);
    if (first.length() < 3)
    {
        cout << "First name must be at least three letters" << endl;
    }
    else
    {

        cout << "Enter last name" << endl;
        cin >> last;
        //lastlen = strlen (last);
        //cin.getline (last, 20);
        //getline (cin, last);


       if (last.length() < 2)
        {
            cout << "Last name must be at least two letters" << endl;
        }
        else

        {

            lastlen = last.length();

            //last [1] = toupper(last [1]);

            cout << "Your Star Wars name is: " << first.substr (0,3) << "-" << last.substr (lastlen -2, 2) << endl;
            //first.resize (3);
            //last.copy(str, strlen -2, 2)
            //last.resize (2);


        }
    }

    cout << first << " " << last << str << endl; //used to check array variables. not part of code



}
Why not the toupper function?
Last edited on
Just figured it out when I saw your reply. Thank for you help.
Topic archived. No new replies allowed.