Stream line reading in incorrectly

I'm trying to read in information to various variables. Some of them have spaces so I have to use getline. I'm getting two issues. The information is not getting read in correctly and the array loop i'm using is re-reading the information.

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
        string tempName;
	string tempAddress;
	int tempCost;
	int tempNumber;
	ifstream myFile("text.txt");
	if (myFile.is_open())
	{
		for (int x = 0; x < 5; x++)
		{		
				getline(myFile, tempName);
				getline(myFile,tempAddress);
				myFile>> tempCost;
				myFile>> tempNumber;

				
				person[x].setName(tempName);
				person[x].setAddress(tempAddress);
				person[x].setCost(tempCost);
					
                                person[x].setNumber(tempNumber);
		}
	}
	else
	{
		cout << "Unable to open file." << endl;
	}
	myFile.close();


Here is two examples that I was trying to read in:

Joe Smith
14 cherry street
5
100
Hillary Wilkens
52 main lane
134
3

I get really weird outputs when I print out the information and the variables never store the right data. Is it because i'm mixing cin and getline? What can I do to fix this? Thanks!
Last edited on
Hello TeebqneX,

Yes when mixing cin and getline or using "myFile" in this case you will need to follow myFile>> tempNumber; and essentially precede the "getline" with:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Needs header file <limits>

With either "cin >>" or "myFile >>" it will not extract the "\n"from the input buffer, so the first read from "getline" will extract the "\n" from the input buffer first giving you the problem you have. Thus the second read with "getline" will extract the wrong information into "tempAddress".

Hope that helps,

Andy
Hello TeebqneX,

When I tried to test your code I found several parts missing like:

The header files.
The class definition.
Is the posted code part of main or a function?

Nowhere in your posted code have you defined a variable instance of your class. using person[x].?? does not work unless you have defined className person somewhere. Now if "person" is the name of the class this is the wrong way to code this. If "person" is an instance of the class, where was it defined and how would this bit of code know about it?

Andy
I got it working :) It had use a couple more getlines to clear out the >> so that it didn't read in blank endlines. I also just put in a check to see when it reached the end of the file to not reread old information. Thanks for your help :)
Hello TeebqneX,

An update to my bad mistake. when I said to use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Needs header file <limits> I forgot to change std::cin.ignore(..) to myFile.ignore(..). It get me every time I do this because I normally use it with "std::cin".

I am glad you got the program working, but extra "getlines" are not the best way to do this.

Changing the for loop to a while loop like this: while (getline(myFile, tempName)) will accomplish the same thing as checking for EOF inside the for loop. Also change the last "person[x]" to "person[x++]" and define and initialize "x" at the beginning of the program.

Hope that helps,

Andy
Topic archived. No new replies allowed.