Solved

Problem with reading into a struct - getline

Posted on 2004-03-21
5
1,441 Views
Last Modified: 2013-12-14
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].Username); //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(114): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
h:\LibraryV1\BookMgr.cpp(114): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'
h:\LibraryV1\BookMgr.cpp(114): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : 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].Username,'*');
 I get these errors with the same line:
h:\LibraryV1\BookMgr.cpp(114): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
h:\LibraryV1\BookMgr.cpp(114): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'
h:\LibraryV1\BookMgr.cpp(114): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,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
0
Comment
Question by:aideenobrien2000
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10643641
What is AddWithdrawal[i].Username type?
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 200 total points
ID: 10643658
This code fragment is compiled if AddWithdrawal[i].Username has std::string type.
0
 
LVL 30

Expert Comment

by:Axter
ID: 10643704
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;
0
 
LVL 30

Accepted Solution

by:
Axter earned 300 total points
ID: 10643708
You already are using Line as a temporary variable.
So you could also do the following:
getline(inp,line);
strcpy(AddWithdrawal[i].Username, line.c_str());
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 10644501
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now