aideenobrien2000
asked on
Problem with reading into a struct - getline
I want to read information from a text file into an array of structs of type AddWithdrawal. I got this code from another question on this site, but it won't work for me (i don't think that it's anything to do with the code, but something to do with my compiler maybe?) I'm using Visual C++.Net
ifstream inp;
string line;
inp.open(FileName);
for(int i=0;i<2;i++)
{
getline(inp,AddWithdrawal[ i].Usernam e); //ERRORS ARE WITH THIS LINE
// use stringstream to read entire line
getline(inp,line);
stringstream ss(line);
ss >> AddWithdrawal[i].BookID;
cout << AddWithdrawal[i].Username << endl;
cout << AddWithdrawal[i].BookID << endl;
}
When I try and compile that code, I get these errors for the first getline statement:
h:\LibraryV1\BookMgr.cpp(1 14): error C2780: 'std::basic_istream<_Elem, _Traits> &std::getline(std::basic_i stream<_El em,_Traits > &,std::basic_string<_Elem, _Traits,_A lloc> &,const _Elem)' : expects 3 arguments - 2 provided
h:\LibraryV1\BookMgr.cpp(1 14): error C2784: 'std::basic_istream<_Elem, _Traits> &std::getline(std::basic_i stream<_El em,_Traits > &,std::basic_string<_Elem, _Traits,_A lloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem, _Traits> &' from 'std::ifstream'
h:\LibraryV1\BookMgr.cpp(1 14): error C2784: 'std::basic_istream<_Elem, _Traits> &std::getline(std::basic_i stream<_El em,_Traits > &,std::basic_string<_Elem, _Traits,_A lloc> &)' : could not deduce template argument for 'std::basic_string<_Elem,_ Traits,_Ax > &' from 'char [10]'
Then if I try and add a third argument (my delimiter)
getline(inp,AddWithdrawal[ i].Usernam e,'*');
I get these errors with the same line:
h:\LibraryV1\BookMgr.cpp(1 14): error C2780: 'std::basic_istream<_Elem, _Traits> &std::getline(std::basic_i stream<_El em,_Traits > &,std::basic_string<_Elem, _Traits,_A lloc> &)' : expects 2 arguments - 3 provided
h:\LibraryV1\BookMgr.cpp(1 14): error C2784: 'std::basic_istream<_Elem, _Traits> &std::getline(std::basic_i stream<_El em,_Traits > &,std::basic_string<_Elem, _Traits,_A lloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem, _Traits> &' from 'std::ifstream'
h:\LibraryV1\BookMgr.cpp(1 14): error C2784: 'std::basic_istream<_Elem, _Traits> &std::getline(std::basic_i stream<_El em,_Traits > &,std::basic_string<_Elem, _Traits,_A lloc> &,const _Elem)' : could not deduce template argument for 'std::basic_string<_Elem,_ Traits,_Ax > &' from 'char [10]'
This is what I've included at the start of the class
#include <string>
#include <vector>
using namespace std;
#include "Structs.h" //headerfile I've created
#include "BookMgr.h" //headerfile I've created
#include <iostream>
#include <fstream>
#include <sstream> // for stringstream
Thanks
ifstream inp;
string line;
inp.open(FileName);
for(int i=0;i<2;i++)
{
getline(inp,AddWithdrawal[
// use stringstream to read entire line
getline(inp,line);
stringstream ss(line);
ss >> AddWithdrawal[i].BookID;
cout << AddWithdrawal[i].Username << endl;
cout << AddWithdrawal[i].BookID << endl;
}
When I try and compile that code, I get these errors for the first getline statement:
h:\LibraryV1\BookMgr.cpp(1
h:\LibraryV1\BookMgr.cpp(1
h:\LibraryV1\BookMgr.cpp(1
Then if I try and add a third argument (my delimiter)
getline(inp,AddWithdrawal[
I get these errors with the same line:
h:\LibraryV1\BookMgr.cpp(1
h:\LibraryV1\BookMgr.cpp(1
h:\LibraryV1\BookMgr.cpp(1
This is what I've included at the start of the class
#include <string>
#include <vector>
using namespace std;
#include "Structs.h" //headerfile I've created
#include "BookMgr.h" //headerfile I've created
#include <iostream>
#include <fstream>
#include <sstream> // for stringstream
Thanks
What is AddWithdrawal[i].Username type?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Your error shows that Username is of type char Username[10].
You can either change it to type std::string as AlexFM has suggested, or use a temporary std::string variable, and then copy it to Username.
You could also use the input operators, but then you'll only get the first word.
inp >> AddWithdrawal[i].Username;
You can either change it to type std::string as AlexFM has suggested, or use a temporary std::string variable, and then copy it to Username.
You could also use the input operators, but then you'll only get the first word.
inp >> AddWithdrawal[i].Username;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also, make sure that you #include <string> to get this version of the getline function. I know that it doesn't make much sense that the std::string class would be defined at all without #include <string> being there, but I've run into it before. Good luck.