Streaming to array

Having a bit of trouble with streaming, ive done it before with no problem..till now. its saying "no operator '>>' matches these operands"
The code is
file >> stateName[i] >> pop[i] >> electors;
Could you post your full code?
no operator '>>' matches these operands

it means that the stream extraction operator >> has to be overloaded for each of the datatypes corresponding to variables stateName[i], pop[i], electors
heres my full 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
std::string path = "C:\\Users\\Lord Brebo\\Desktop\\us-pop-by-state-with-electors-2015.txt";
		std::ifstream file;
		std::string *stateName;
		int *pop, electors, siz=1, count=0, totalPop=0, i=0;
		double percentWithinSD = 0.0, meanPop = 0.0, SDPop = 0.0, pos=0.0, neg=0.0;
		//two arrays, one for stateName and one for poplulation
		stateName = new std::string[siz];
		pop = new int[siz];
		file.open(path);

		if (!file)
		{
			cout << "unable to open file: " << path << endl;
			return -1;
		}
		file >> siz;
		while (!file.eof())
		{
			file >> stateName[i] >> pop[i] >> electors;
			++i;
			++count;
		}
		file.close();
		//calculate mean
		while (i=0, pop[i] < siz, i++)
		{
			totalPop += pop[i];
		}
		meanPop = totalPop / siz;
		//calculate Variance

		cout << "US Population Statistics as of 2015 (" << siz << " states)" << endl;
		cout << "  Total population: " << totalPop << endl;
		cout << "  Population mean: " << meanPop << endl;
		cout << "  Population standard deviation: " << SDPop << endl;
		cout << "  States whose population is within .5 standard deviations of the mean:" << endl;
		//list states within .5 Standard deviation
		while (i = 0, i < siz, i++)
		{
			if (neg >= pop[i] <= pos)
			{
				cout << stateName[i] << endl;
			}
		}
		//calculate % of states that fit ^^^^ that

		cout << percentWithinSD << "% of the states are within .5 standard deviations of the mean population" << endl;
			return 0;
Use the prescribed includes, don't assume since something else is bringing it in.

If you're using std::string: #include <string>
Another point: don't loop on eof:

http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong

With stateName and pop , just use a std::vector rather than a new allocated array.

Maybe you are not allowed to use std::vector, if so that is a shame - I wish they would stop teaching C to C++ students.

siz is 1 , so the arrays have size of 1? Any point in that?
Last edited on
Topic archived. No new replies allowed.