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:
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:
'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
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