Thread: getline function not working

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    getline function not working

    Can someone please tell me which header to use for using getline() in C++. I have used

    Code:
    #include<iostream>
    #include<string>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    none of them seem to be working.

    Here is the entire code that I have written so far

    Code:
    #include "stdafx.h"
    #include<iostream>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    using namespace std;
    
    class TextQuery
    {
        typedef map<string, set<string::size_type> > word_map;
        TextQuery(ifstream &file)
        {
            FillLineVector(file);
            BuildMap(file);
        }
    
        void query(string);
    private:
        word_map map;
        vector<string> LineVector;
        void FillLineVector(const ifstream&);
        void BuildMap(const ifstream&);
    };
    
    void TextQuery::FillLineVector(const ifstream& file)
    {
        string line;
        while(getline(file,line)); //error:getline identifier not found
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include <string>
    Kurt

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try removing the const
    Code:
    void TextQuery::FillLineVector(ifstream& file)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by oogabooga View Post
    Try removing the const
    Still not working. Got a whole new bunch of errors

    Code:
    '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'
    '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

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't know what to tell you. This works:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    void func(ifstream& file) {  // doesn't work with const, though
        string line;
        while (getline(file,line))
            cout << line << '\n';
    } 
    
    int main() {
        ifstream fin("data.txt");
        func(fin);
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by oogabooga View Post
    I don't know what to tell you. This works
    He is missing the
    Code:
    #include <fstream>
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with getline function
    By chickenlittle in forum C++ Programming
    Replies: 7
    Last Post: 10-13-2010, 01:04 PM
  2. my getline funtion is not working?
    By steals10304 in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2009, 03:26 PM
  3. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline function
    By JamMan in forum C++ Programming
    Replies: 11
    Last Post: 11-25-2001, 05:29 PM