Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

unintelligence

macrumors newbie
Original poster
Hey everybody, this is not exactly related to Mac OS X, but I have a quick question regarding file input in C++ for a final project in one of my classes.

Basically, I'm given a data file with a bunch of customers' information in it, and I am writing a loop to pick up each name, billing and shipping address, ID number, etc. from it

The format for the data is:
[id] [customer name]
[billing address]
[shipping address]

Here is a sample of the data:
Code:
11112222  Jack J Jones
21 W. Crenshaw Blvd.  
Los Angeles
CA  92001   USA
21345 De Anza St. 
Torrance
CA  90056  USA
22333333 Nancy K Brown
210 Adams Blvd.
Los Angeles 
CA  92001 USA
210 Adams Blvd.
Los Angeles 
CA  92001 USA
22224444 Antony S Detrely
310 W. 5th St.
Los Angeles
CA 92010 USA
310 W. 5th St.
Los Angeles
CA 92010 USA

For some reason, I cannot pick up the data correctly. It compiles just fine, so I must have some kind of error in logic. Here is the code I'm using:

Code:
void Store::readKnownCustomers()
{
	long int id;
	string firstname;
	char middle;
	string lastname;
	int addressNumber;
	string street;
	string city;
	string state;
	int zip;
	string country;
	Person tempPers;
	Customer tempCust;
	Address tempAddr;
	ifstream fin;
	string response;
	
	string fileName;
	cout << "Please enter name of customer information data file: " << endl;
        cin >> fileName;
	fin.open(fileName.c_str());
	
	cout << "Building customer database..." << endl;
	
	if(fin.fail())
		cout << "Unable to open customer data file: System error." << endl;
		cout << "Do you wish to try again? (yes/no)" << endl;
		cin >> response;
		if(response == "yes")
			readKnownCustomers();
	else if(fin.peek() == -1)
	{
		cout << "Unable to open customer data file: No data in file." << endl;
		cout << "Do you wish to try again? (yes/no)" << endl;
		cin >> response;
		if(response == "yes")
			readKnownCustomers();
	}
	else
	{
		cout << "Success!" << endl;
		fin >> id;
		while(!fin.eof())
		{
			fin >> firstname;
			fin >> middle;
			getline(fin, lastname);
			fin >> addressNumber;
			getline(fin, street);
			getline(fin, city);
			fin >> state;
			fin >> zip;
			fin >> country;
		
			tempPers.setIDNumber(id);
			tempPers.setFirstName(firstname);
			tempPers.setLastName(lastname);
			tempPers.setMiddleInitial(middle);
		
			tempCust.setPerson(tempPers);
		
			tempAddr.setHouseNumber(addressNumber);
			tempAddr.setStreet(street);
			tempAddr.setState(state);
			tempAddr.setCity(city);
			tempAddr.setZip(zip);
			tempAddr.setCountry(country);
		
			tempCust.setBilling(tempAddr);

			fin >> addressNumber;
			getline(fin, street);
			getline(fin, city);
			fin >> state;
			fin >> zip;
			fin >> country;
		
			customers.push_back(tempCust);
			
			fin >> id;
		}
	}
}

'customers' is a Customer vector

Can anybody see something that might be affecting this process?

Thank you everybody for your help, it is much appreciated : )

P.S.> If you guys need to see some .h or .cpp files, just let me know
 

Attachments

  • customers.txt
    351 bytes · Views: 104

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
Is the problem occurring where you get the address number then the rest of the address? If it is that suggests that the getline() function isn't starting from where the last >> operation ended, but rather from the beginning of the last line in the file that was read. If that's happening, the solution is to call getline() to read the whole address in one chunk then split it into a number and street by splitting the string into two on the first space.
 

unintelligence

macrumors newbie
Original poster
thanks wrldwzrd so much for your help. it turns out that i had forgotten to assign the second inputted address (the shipping address) to the customer object. i put one line of code in to do that and it fixed it. however, i am considering rewriting the function to incorporate your suggestion, because I'm having a tough time outputting the data to the console in an aesthetically-pleasing way. once again, thanks!
 

dogeye

macrumors newbie
Jul 14, 2006
5
0
some other problems I noticed:
1. the if statement has no curly braces.
2. after some error, or failure, it shouldn't call the same function again cause it will become recursive function call. It should exit, and do the re-run outside(in main function) again.
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
some other problems I noticed:
1. the if statement has no curly braces.
2. after some error, or failure, it shouldn't call the same function again cause it will become recursive function call. It should exit, and do the re-run outside(in main function) again.
If statements don't need curly braces if they only contain one line of code. I agree with you on the second point though.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.