Image

Imageswos wrote in Imagecpp 😧blank

Short question

I'm a new poster here, though I've been a member for a week or so now. I usually don't ask a lot of questions about anything, but I've searched high and low, in vain, for the answer to my following question.

I'll throw my code and question behind a cut.


I'm writing a contact manager for my introductory computer science class. It has the functionality of adding, removing, viewing, and deleting contacts.

As part of the specifications for the program, it must receive input from the user, which is thrown into a structure containing all of the elements of a contact.

The structure itself:

struct contact{
char lName[24];
char fName[24];
...
};

I cut the struct short here to save on space.

In, any case, I've created a data file (manually) with these fields:

Doe$John$555-0001$jdoe@hotmail.com$123 University Avenue$New York City, NY$
Doe$Jane$555-0002$jane@hotmail.com$321 High Street$Baltimore, MD$

After reading about strings and character data, I decided the getline() method would be the best option for retreiving the contents of these fields.

The problem I have is tearing apart the string and placing each delimited element into a corresponding structure member. In the final project, I will need to do this with an array of structs. But for now, I'm only concerned with one: myContact.

#include
#include

using namespace std;

struct contact{
char lName[24];
char fName[24];
char sAddr[128];
};

int main()
{

ifstream iFile("/home/jaustin/projects/experimental/contact.dat");
char myBuffer[256];
contact myContact;

while(!iFile.eof())
{
iFile.getline(myBuffer, 256, '$');
cout << myBuffer << endl;
}

iFile.close();

return 0;
}

I'm aware there are no assignment statements within my code. At this point, I was only concerned with the contents of myBuffer after each run through the while statement, until the end of file was encountered.

I have a hunch that I need repetition to iterate through each of the delimited elements, but I'm not thinking clearly enough right now.

If anyone has any kind words or suggestions, they would be much appreciated.