-1

I'm working on an address book application, and the portion I'm really struggling with is getting text from a .txt file and assigning the correct variable to each piece of text. each piece is delineated with a tab.

I'm really struggling with this one. First off, I'm not sure if my logic is 100% sound on my approach, but nevertheless, this is how it's cropped up in my head.

What I'd like to do is run a loop that looks at the text line by line, each line being assigned to its respective variable.

I know my current code isn't right, and the logic isn't fully fleshed out, but this is what I have.

EDIT: I forgot to mention the other variables and their respective datatypes. first name, last name, birth month/day/year, address, city, state, zip/postal code, phone number. they're virtually all strings with the exception of the birth month/day/year and zip code/postal code which are integers.

EDIT** the text file goes something like this

Joe     Shmoe     1     1     1980     123Main     Capital     CA     90210     123-456-7890
Jane     Doe     2     5     1960     321Elm     Boise     ID     12345     987-654-3210

so on and so forth. What I need to do is read through the text file and fill the array elements for each attribute (first, last, month, day, year, address, city, state, zip, phone)

void extPersonType::getInfo()
{
readFile.open("input.txt",ios::in);
if (readFile.is_open())
{
    while (readFile.good())
    {
        for (int i = 0; !readFile.eof(); i++)
        {
            getline(readFile,lines[i]);
        personInfo[i].setFirstName(lines[i]);//change this
        cout << personInfo[i].getFirstName();
        }
    }
    readFile.close();
}

If you could help me out on this I'd appreciate it.

Justin Ertley
  • 93
  • 1
  • 4
  • 9

1 Answers1

0
for (int i = 0; !readFile.eof(); i++)
        {
            getline(readFile,lines[i],'\t');
        personInfo[i].setFirstName(lines[i]);
        cout << "First Name: "<<personInfo[i].getFirstName()<< endl;

        getline(readFile, names[i],'\t');
        personInfo[i].setLastName(names[i]);
        cout << "Last Name: "<<personInfo[i].getLastName()<<endl;

        getline(readFile, addresstmp[i],'\t');
        address[i].setAddress(addresstmp[i]);
        cout << "Address: "<< address[i].getAddress() << endl;
        getline(readFile, city[i],'\t');
        address[i].setCity(city[i]);
        cout << "City: "<< address[i].getCity()<<endl;
        getline(readFile,state[i],'\t');
        address[i].setState(state[i]);
        cout << "State: "<<address[i].getState()<<endl;
        getline(readFile,zip[i],'\t');
        address[i].setZip(zip[i]);
        cout << "Zip: "<<address[i].getZip()<<endl;
        getline(readFile,phoneNumber,'\t');
        cout << "Phone: "<<phoneNumber<<endl;
        getline(readFile,contactType);
        cout <<"Contact type: "<<contactType<<endl<<endl;

        }

    }
    readFile.close();

this is what I was looking for. getline(file, string, deliniation) solved it.

Justin Ertley
  • 93
  • 1
  • 4
  • 9