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;
}
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;
}