Help with getting input from file

I am trying to get the information from the file into a vector so it can then be sorted. However when reading in from the file I get the following for every line in the file:
,,0,0,0
,,0,0,0
.......

The file contains only 10 different city so it should only have 10 lines in the output but it appears to output for each piece of information.

I know some of the stuff in the code is probably a little messy and that is because I have been trying everything I can think of to get it to work but I think it is time I have someone else look at the code. Please let me know if I am doing something wrong as I feel I am very close but am just missing something.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

class Information {
	public:
		Information();
		~Information(){}
		void setCity(string city)
		{
			_city = city;
		}

		void setState(string state)
		{
			_state = state;
		}

		void setLongitude(double longitude)
		{
			_longitude = longitude;
		}

		void setLatitude(double latitude)
		{
			_latitude = latitude;
		}

		void setPopulation(double population)
		{
			_population = population;
		}

		string getCity()
		{
			return _city;
		}

		string getState()
		{
			return _state;
		}

		double getLongitude()
		{
			return _longitude;
		}

		double getLatitude()
		{
			return _latitude;
		}

		double getPopulation()
		{
			return _population;
		}
		void printInfomationInfo();

	private:
		string _city, _state;
		double _longitude, _latitude, _population;
};
Information::Information()
{
	_population = 0.0;
	_longitude = 0.0;
	_latitude = 0.0;
}
void Information:: printInfomationInfo()
{
	cout << _state << "," << _city << "," << _longitude << "," << _latitude << "," << _population << endl;
}

int main ()
{
	ifstream inFile;
	ofstream outFile;
	string inputFileName, line;
	const int size = 1000;
	string inputFile[size];
	double temp, latitude, longitude, population;
	int current, startValue, currentMin, compareValue;
	int count = 0;
	int position = 0;
	string byCity = "byCity";
	string byLongitude = "byLongitude";
	string input[size], city[size], state[size];
	double values[size];
	vector<Information> information;
	Information *info;
	info = new Information;

	cout << "Please enter the name of the file: " ;
	cin >> inputFileName;
	
	// Open file
	inFile.open(inputFileName.c_str());

	// If file opens successfuly read in data from file
	if(inFile.good())
	{		
		cout << "File was opened!" << endl;
		while (inFile.good())
		{
			if (position = 19)
			{ 
				information.push_back(*info);
				info = new Information;
				position = 0; 
			}
			getline(inFile, line, '|' );
			input[position] = line;

			if (position = 1)
			{
				info->setState(input[position]);
			}

			if (position = 9)
			{
				latitude = ::atof(input[position].c_str());
				info->setLatitude(latitude);
			}
			if (position = 10)
			{
				longitude = ::atof(input[position].c_str());
				info->setLongitude(longitude);
			}
			if (position = 16)
			{
				population = ::atof(input[position].c_str());
				info->setPopulation(population);
			}
			if(position = 18)
			{
				info->setCity(input[position]);
			}
			else
				line = "";
			position++;
		}

                vector<Information>::iterator n;
		for ( n = information.begin(); n != information.end(); n++)
		{
			n->printInfomationInfo();
		}
        }
		//End read in of data 




Last edited on
Bump
When you check the value of "position" inside the while loop, you use a single "=", which assigns the value. Comparison is performed with "==".
Last edited on
Topic archived. No new replies allowed.