I cannot move onto the next line in the file

I have this code where I cannot move onto the next line I keep feeding my variables the same stuff every time for example
Two-Story 5 43 2012 3670 389000 8960
Two-Story 5 43 2012 3670 389000 8960
Two-Story 5 43 2012 3670 389000 8960

When I should be getting something like this
Two-Story 5 4 3 2012 3670 389000.00 8960
Single-Story 3 2 2 2003 1450 135000 2250
Split-Condo 3 2 2 1999 1630 165300 2670

I have this function called print and it just prints simply
as so
1
2
3
4
5
6

void houseType::print() {
	cout << style << " " << numOfBedrooms << " " << numOfBathrooms << numOfCarsGarage << " " << yearBuilt << " " << finishedSquareFootage << " " << price<<" "<<tax<<endl;
}



I just loop through them in my main function
1
2
3
4
for (int i = 0; i < 7; i++) {
		properties[i].readData();	
		properties[i].print();
		


PLEASE HELP ME

1
2
3
4
5
6
7
8
9
10
11
12
13
  void houseType::readData() {
	ifstream myfile;
	myfile.open("Text.txt");

	if (!myfile.is_open()) {
		exit(EXIT_FAILURE);
	}

	
		myfile >> style >> numOfBedrooms >> numOfBathrooms >> numOfCarsGarage >> yearBuilt >> finishedSquareFootage >> price >> tax;
		
		
}
Last edited on
Every time that "void houseType::readData()" is called, the file is opened at the beginning point, and is closed when the function loses scope (http://www.cplusplus.com/forum/beginner/107067/).

To share an opened file between objects you could pass-by-reference the ifstream object to readData.

You could also read the file outside the object and pass the information in.

Or pass a number into readData that contains the seekg point to start reading the object's data from, then return the seekg point of where it stops...

There's probably a dozen other ways to go about it, but these would be easy to implement...
Last edited on
Every time you go to read data into your properties variable, you're opening the file. This means you open the file, you take in the first line, the function finishes (meaning the variable that opened the file is destroyed), and then repeat.

You need to either have a variable to keep track of which line your on and skip to that line every time you enter the function...

or you can open the file outside the function, so that you're not constantly reopening the file every time you want to take in a line. Every time the file opens, you're starting at the beginning of the file!
Topic archived. No new replies allowed.