Click here to Skip to main content
14,974,887 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm getting getline error on my code when I run it in Visual Studio. But when I run it on DevC++ it's working like a charm. Error message : E0020 identifier "getline" is underfined. What's going on? Here's my code:

C++
<pre>
//Read from a file

#include <iostream>
#include <fstream>          //Must have
using namespace std;

int main(){
	string mytext;
	ifstream myreadfile("code.txt");        //Must be on same folder as compiler saving path
	
	while (getline (myreadfile, mytext)){        //While loop to be able to print the data
		cout<<mytext;
	}
	myreadfile.close();                 //Close the file after
	
	return 0
;}


What I have tried:

I don't know what I should do cause it's working on different compiler so, is it my fault or?
Posted
Updated 7-May-21 0:03am
v4
Comments
CPallini 7-May-21 5:50am
   
Could you please post the exact error message?
Nektarios Kouromichelakis 7-May-21 5:54am
   
E0020 identifier "getline" is underfined

I did a bit of a research and found a solution. Visual Studio needs the <string> library to identify the getline command.

Here's the code refactored and fixed :

C++
<pre>//Read from a file

#include <iostream>
#include <fstream>         //Must have
#include <string>          //Fixed with the string library
using namespace std;

int main(){
	string mytext;
	ifstream myreadfile("code.txt");        //Must be on same folder as compiler saving path
	
	while (getline (myreadfile, mytext)){        //While loop to be able to print the data
		cout<<mytext;
	}
	myreadfile.close();                 //Close the file after
	
	return 0
;}
   
Add
C++
#include <string>

At the top of your file.
See, for instance, c++ - getline: identifier not found - Stack Overflow[^]).
   
Comments
Nektarios Kouromichelakis 7-May-21 6:06am
   
That's where I looked to. Thank you :)
CPallini 7-May-21 6:08am
   
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900