trying to use getline to read from file

Hello, I am having trouble trying to use getline to get some data out of a file that was made earlier in the program and put it into an array to be futher processed. ( not finished with the rest yet).When I complie I get the error
Quest 1.cpp||In function 'int main()':|
 Quest 1.cpp|48|error: no matching function for call to 'getline(char [20], char [3])'|
 Quest 1.cpp|51|error: no matching function for call to 'getline(char [20], char [20])'|


this happens at lines 48 and 51 in the second else statement. Any help would be great. Here is the code.


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


char first [20];
char last [20];
int firstlen;
int lastlen;
fstream data_store;
char nfirst [3];
char nlast [20];
char lastone;
char lasttwo;
int main()
{
    data_store.open("first.txt", ios::out);

    cout << "== Star Wars Name Generator ==" << endl;
    cout << "Enter first name" << endl;
    cin.getline (first, 20);
    data_store << first << endl;
    data_store.close();
    firstlen = strlen (first);
    if (firstlen < 3)
    {
        cout << "First name must be at least three letters" << endl;
    }
    else
    {
        data_store.open("last.txt", ios::out);
        cout << "Enter last name" << endl;
        cin.getline (last, 20);
        data_store << last << endl;
        data_store.close();
        lastlen = strlen (last);

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

        {
            data_store.open("first.txt", ios::in);
            getline(first, nfirst);
            data_store.close();
            data_store.open("last.txt", ios::in);
            getline(last, nlast);
            data_store.close();


        }
    }

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


}
cin.getline() takes a cstring, no problem there.

getline() takes a c++ string, first is a cstring.
So I changed the getline () to a cin.getline (). now I get the following errors

 Quest 1.cpp||In function 'int main()':|
 Quest 1.cpp|48|error: invalid conversion from 'char*' to 'int'|
 Quest 1.cpp|48|error:   initializing argument 2 of 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'|
 Quest 1.cpp|51|error: invalid conversion from 'char*' to 'int'|
 Quest 1.cpp|51|error:   initializing argument 2 of 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'|
||=== Build finished: 4 errors, 0 warnings ===|


at lines 48 and 51.
first
,
nfirst
,
last
and
nlast
are all char but the error is saying that there is an
invalid conversion from char* to int
. Not sure what is wrong now. Thanks for the help and here is the new code.

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


char first [20];
char last [20];
int firstlen;
int lastlen;
fstream data_store;
char nfirst [3];
char nlast [20];
char lastone;
char lasttwo;
int main()
{
    data_store.open("first.txt", ios::out);

    cout << "== Star Wars Name Generator ==" << endl;
    cout << "Enter first name" << endl;
    cin.getline (first, 20);
    data_store << first << endl;
    data_store.close();
    firstlen = strlen (first);
    if (firstlen < 3)
    {
        cout << "First name must be at least three letters" << endl;
    }
    else
    {
        data_store.open("last.txt", ios::out);
        cout << "Enter last name" << endl;
        cin.getline (last, 20);
        data_store << last << endl;
        data_store.close();
        lastlen = strlen (last);

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

        {
            data_store.open("first.txt", ios::in);
            cin.getline(first, nfirst);
            data_store.close();
            data_store.open("last.txt", ios::in);
            cin.getline(last, nlast);
            data_store.close();


        }
    }

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


}
It looks like I was using the second set of cin.getline wrong. I have the error fixed but now I can not figure out how to get the data out of first and last and into nfirst and nlast. I may not be using the cin.getline corrctly or should be using something else. Ultimately I will try to get the first 3 letters from the file first and the last 2 letters from the file last. Here is the code. Thanks

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


char first [20];
char last [20];
int firstlen;
int lastlen;
fstream data_store;
char nfirst [3];
char nlast [20];
char lastone;
char lasttwo;
int main()
{
    data_store.open("first.txt", ios::out);

    cout << "== Star Wars Name Generator ==" << endl;
    cout << "Enter first name" << endl;
    cin.getline (first, 20);
    data_store << first << endl;
    data_store.close();
    firstlen = strlen (first);
    if (firstlen < 3)
    {
        cout << "First name must be at least three letters" << endl;
    }
    else
    {
        data_store.open("last.txt", ios::out);
        cout << "Enter last name" << endl;
        cin.getline (last, 20);
        data_store << last << endl;
        data_store.close();
        lastlen = strlen (last);

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

        {
            data_store.open("first.txt", ios::in);
            cin.getline(nfirst, 3);
            data_store.close();
            data_store.open("last.txt", ios::in);
            cin.getline(nlast, 20);
            data_store.close();


        }
    }

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


}
Last edited on
The compiler is complaining that there's no function called "getline" that takes in two strings.

first and nfirst are both character arrays (pointer to string equivalent).

You should look at the istream reference. Here's the link to istream::getline(), the function you're using.

http://cplusplus.com/reference/iostream/istream/getline/

Also, please refrain from using the using namespace std, as you're polluting the standard namespace. Consider using the prefix std:: instead.
Hi Nexius, I have not gotten passed using std; in the book yet. I was not even aware of the std::. I will look into it. Thanks
Topic archived. No new replies allowed.