How get to work?

I am fairly new to programming, and this website, and am trying to get a simple program to work. I am trying to read data from a file, and for now, output it, and eventually see what i can do with it. I cant even seem to get it to just output the data.

I keep getting the error:
Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) t:\visual studio 2008\projects\getlineexample\getlineexample\main.cpp 18 getlineExample

Any help would be appreciated. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>

using namespace std;

int main(){

	ifstream popFileIn;
	string data;

	string popFileName = "T:\\Visual Studio 2008\\Projects\\Population\\PopulationData.txt";
	popFileIn.open (popFileName.c_str());

	if (popFileIn.fail()){
		cout << "Failed to open file." << endl;
	}
	else{
		popFileIn >> data;
		cout << data;
	}


return 0;

}
add this to your code:

#include <string>
Dammit! Thanks.
I now cannot seem to get it to display the data in a column. It appears to read it but only display's the last line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){

	ifstream popFileIn;
	string data;

	string popFileName = "T:\\Visual Studio 2008\\Projects\\Population\\PopulationData.txt";
	popFileIn.open (popFileName.c_str());

	if (popFileIn.fail()){
		cout << "Failed to open file." << endl;
	}
	else{
		while (popFileIn.good()){
			getline(popFileIn, data, '\n');
			cout << data << endl;
			popFileIn.ignore();
		}
	}

return 0;

}
Topic archived. No new replies allowed.