getline is reading in ALL white spaces until endl

I have a file, that has a list of about 300 cities and its cost of living index. Example: 115.3 San Jose, Ca etc. I wrote a program that reads in this data:

while (inputFile.peek() != EOF)
{

inputFile >> *(costIndexPtr + i);
while (inputFile.peek() == ' ')
inputFile.get();
getline(inputFile, *(cityPtr + i));
i++; //used to keep track of how many cities total
}

I also wrote code to output the data:

for (int i = 0; i < (num - 1); i++)
{
cout << left << setw(7) << costIndexPtr[i] << " 1" << cityPtr[i]
<< "1" << endl;
outputFile << left << setw(7) << costIndexPtr[i] << " "
<< cityPtr[i] << endl;
}

However, when the actual data the is output is as follows:

104.6 1Winchester, VA-WV
1
108 1Wilmington, NC
1
108.1 1Wilmington, DE
1

As you can see, the inputFile reads in the cost of living index, the city's name and state AND all the following whitespaces. How do I fix this??
If you wanted to delimit by whitespace, why did you use std::getline instead of operator>>?
I was under the impression that if I wanted to include a string with spaces in between, I need to use getliine.
Yes, but you need to know when that string ends though. What does your input file look like?
try this :


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
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main(int argc , char * argv[])
{
	std::ifstream file("cities.txt");

	double* costIndexPtr = new double[3];
	std::string* cityPtr = new  std::string[3];
	std::string* statePtr = new  std::string[3];
	int i = 0;

	if(!file.good())
		std::cout << "Could not read file" << std::endl;

	while (!file.eof())
	{
		file >> costIndexPtr[i];
		file >> cityPtr[i];
		file >> statePtr[i ++];
	}
	std::cout << "End read" << std::endl;
	for (int i = 0; i < 3; ++i)
	{
		cout << left << setw(7) << costIndexPtr[i] << " 1" << cityPtr[i] 
		<< " 1" << statePtr[i] << endl;
	}

	system("pause");
	return EXIT_SUCCESS;
}


and I change your file content by this :

1
2
3
104.6 Winchester VA-WV
108.0 Wilmington NC
108.1 Wilmington DE
One way is to read it in and split the city and state apart like this


1
2
3
4
5
6
7
8
9
10
11
12
13
14
double ANum=0.0;
string CityState="";
string City="";
string State="";

InFile >> Anum;
InFile >> CityState;

City=CityState;
State=CityState;
City.erase (CityState.size()-4,4);
State.erase (0,CityState.size()-2);
cout << City << endl;
cout << State << endl;
Topic archived. No new replies allowed.