-4

i'm trying to copy members from one structure to another structure, but i have no clue how to do so.

struct StudentTxt
{
    int ID;
    string lname;
    string fname;
    string initial;
    int age;
    double balance;
};
struct StudentBin
{
    int ID;
    int age;
    double balance;
    char fullname[50];
};

i read the file and store all the data into the first structure, and after that i combined the lname, fname,initial into one string.

the question is i'm trying to copy the string to the fullname in the second structure, and also the ID, age, Balance.

can someone guide me to the right path.

any help will be appreciated.

user3080461
  • 33
  • 3
  • 8
  • You're converting from one structure to another. The problem has nothing to do with anything special about *your* structures. *Write code* that builds the structure content you *want* from the structure content you *have*. You have tried that, right? Certainly so. *Post what you have tried so far*. – WhozCraig Apr 30 '14 at 17:58

4 Answers4

2

How about writing a function to perform the translate and copy?

void studentCopy( StudentTxt const * pSrc, StudentBin * pDst ) {
    pDst->ID = pSrc->ID;
    pDst->age= pSrc->age;
    pDst->balance = pSrc->balance;

    string const name = pSrc->fname + pSrc->initial + pSrc->lname;
    size_t const dstLen = sizeof( pDst->fullname );
    strncpy( & pDst->fullname, name.c_str(), dstLen );
    pDst->fullname[ dstLen - 1 ] = 0;    // NUL terminate
}
Arun
  • 19,750
  • 10
  • 51
  • 60
  • thanks, can you explain what does -> mean in your code. – user3080461 Apr 30 '14 at 18:06
  • 1
    +1 for being the only answer that actually saw the general problem. The opening sentence "How about writing a function to perform the translate and copy" on its own would have earned my uptick, as it is the correct solution. Adding an *implementation* is just exceeding the bar. – WhozCraig Apr 30 '14 at 18:10
  • @user3080461: '->' is pointer dereference. `pSrc` is a pointer to a `StudentTxt` object. `pSrc->ID` returns the `ID` value of that referred object. See http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean for further details. – Arun Apr 30 '14 at 19:37
1

u can just declare fullname as std::string and then write fullname= lname+ " "+fname+" "+initial; while converting.

if u have to use char array then do the following:

           strcat(fullname,lname.c_str());
           strcat(fullname,fname.c_str());
           strcat(fullname,initial.c_str());

remember to initialize fullname by fullname[0]=0; before the above operations. you can also use strcat(fullname," "); after each concatenation for proper format.

And then simply copy other attributes of the first structure into the second.

Rakib
  • 7,435
  • 7
  • 29
  • 45
1

Why can't you just use the assignment operator?

// Say these are for the same student
StudentTxt studentATxt;
StudentBin studentABin;

// Copy items over
StudentABin.ID = StudentATxt.ID;
StudentABin.age = StudentATxt.age;
StudentBin.fullname = StudentTxt.fname.c_str()

...etc

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1

For the fullname part:

std::string fullname(
    StudentATxt.lname + " " +
    StudentATxt.fname + " " +
    StudentATxt.initial);

if(fullname.size() < 50)
    strcpy(StudentABin.fullname, fullname.c_str());
PeterSW
  • 4,921
  • 1
  • 24
  • 35
  • `StudentABin.fullname = fullname.c_str();` is a pointer assignment. It becomes invalid as soon as the (stack) object `fullname` goes out of scope. To avoid that, the contents of string need to be copied. – Arun Apr 30 '14 at 19:41
  • Hi @Arun thanks for pointing that out! I've corrected the answer now... – PeterSW Apr 30 '14 at 19:52