1

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

#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

#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
  • What do you mean by "not working"? Be more specific please. Commented May 9, 2012 at 19:03
  • please add code to your question, which is using getline. Commented May 9, 2012 at 19:05

3 Answers 3

2

You need iostream and possibly string if that's the version you're using. Why that doesn't work will have to remain a mystery since you provided no additional information.

Sign up to request clarification or add additional context in comments.

Comments

2

Post your code if you want people to be able to help you. Anyhow, getline should be used as such:

// getline with strings #include <iostream> #include <string> using namespace std; int main () { string str; cout << "Please enter full name: "; getline (cin,str); cout << "Thank you, " << str << ".\n"; } 

As showed in http://www.cplusplus.com/reference/string/getline/

Comments

0

Your functions take ifstream rather than istream, so you would have to include <fstream> to get the definition of that.

However, you're probably better off making the functions more generic by taking istream, unless for some reason they can only work with a file. In that case, you'll just need <iostream> and <string>, which you already have.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.