-1

I used a **char pointer/array to store a list of char values.

I have a method to store the values, but my method to retrieve these values have not been as successful.

This is my method that sets the values...

char **names; 
char *input_name;
int studentNameSize;

void Students::setStudentNames() 
{
    names = new char*[studentNameSize]; 
    for(int i=0; i<studentNameSize; i++)
    {
        names[i] = new char[60];
        cout << "Input name" << i << ": \n";
        cin >> input_name;
        strcpy(names[i],input_name);
        cout << names[i] << "\n";
    }
}

This is my method and it only return the memory size and not the actual values. It returns 0x(size of the array, i.e. 0x03 instead of something like Bob; Charles; Mike

const char** Students::getStudentNames()
{
    for(int i=0; i<this->studentNameSize; i++)
    {
        return this->names[i];
    }
}

Sorry, I'm new to C++ and I need some assistance with the friend ostream& operator<<. Basically I know how to output to file now but I don't get how to use the friend ostream& operator<< method of doing it. I have something like this but I am really lost as to how I can output to file this way.

ostream& operator<< (ostream& ostream, const Students& students)
{
    os << students.getStudentNames();
    return os;
}
user4167396
  • 29
  • 1
  • 7

1 Answers1

2

This right here is not going to do what you expect. A return statement instantly leaves the function, so the first time this loop is met, it will return.

So effectively, this code:

const char** Students::getStudentNames()
{
    for(int i=0; i<this->studentNameSize; i++)
    {
        return this->names[i];
    }
}

Really means this:

const char** Students::getStudentNames()
{
    return this->names[0];
}

I believe what you really want to do is this:

const char** Students::getStudentNames()
{
    return this->names;
}

This bit of code returns the entirety of the this->names variable, which according to the function name getStudentNames is what it does anyway.

As for the serialization part of your question, your code is mostly correct in that instance. It should look like this:

ostream& operator<< (ostream& os, const Students& students)
{
    os << students.getStudentNames();
    return os;
}

And to deserialize:

istream& operator>>(istream& is, const Students& students)
{
    is >> students.names;
    return is;
}

Note that these methods of serialization, when using more complex objects, are much more susceptible to data corruption. Here is some further reading for you.

Hope this helped, good luck!

Community
  • 1
  • 1
phantom
  • 1,457
  • 7
  • 15
  • Thank you for your answer. How do I modify your code so that it can return all the values of names? Would I have to store it some array or something. I would really appreciate it if you could help me out as I am new to C++ – user4167396 Nov 01 '14 at 15:51
  • @user4167396 Sure thing, give me a minute to update my answer. – phantom Nov 01 '14 at 15:51
  • @user4167396 There you go! – phantom Nov 01 '14 at 15:53
  • Thank you for your generosity. It means a lot to me, and I appreciate people like you who are willing to help me out, unlike some users who decide to get rude – user4167396 Nov 01 '14 at 15:54
  • @user4167396 Hey no problem man. Don't forget to accept this answer if it helped you, that way other users from google can find the answer easier! – phantom Nov 01 '14 at 15:55
  • Thanks! As for the ostream operator, how do I use it to store my information into a txt file? Sorry, I am new to C++ – user4167396 Nov 01 '14 at 15:56
  • @user4167396 I haven't had to do this in a couple years, give me a few minutes to remember how to do it, then I'll update my answer! – phantom Nov 01 '14 at 15:58
  • Thanks phantom. It means a lot to me – user4167396 Nov 01 '14 at 16:10
  • @user4167396 There you go. – phantom Nov 01 '14 at 16:13
  • I am getting this error `invalid conversion from 'char**' to 'const char**'` for your `getStudentNames()` method – user4167396 Nov 02 '14 at 09:46
  • @user4167396 if `this->names` is of type `char**`, just change your method to `char** Students::getStudentNames()` e.g. remove the `const` keyword. – phantom Nov 02 '14 at 15:10
  • Thanks Phantom. My `setStudentNames()` method is crashing my program, and I don't even know why :( – user4167396 Nov 02 '14 at 16:50
  • @user4167396 That's kind of out of the scope of the original question...your question has been answered mark it as accepted and create a new question. – phantom Nov 02 '14 at 16:53