• 0

[c++] get and set methods for strings


Question

Hi. I believe this should be a simple question. Basically I have a class, Student. where I want to get a name, and set a name, this is what I got so far:

void Student::setName(char name[])

{

itsName = name;

}

char[] Student::getName();

{

return itsName;

}

itsName is the private member.

char itsName[30]

I don't know how to copy one string to another. Can anybody help me?

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

From the syntax, I assume you're talking about C++.

Use the string class:

void Student::setName(const std::string& name)
{
     itsName = name;
}

std::string Student::getName()
{
     return itsName;
}

Using the private member:

std::string itsName;

Don't forget to include the header file for strings:

#include <string>

Hope that helps,

bwx

Link to comment
Share on other sites

  • 0

thank u both. it worked!

Is this how you use it?

S.setName("name");

cout << "Answer.Name = " << Answer.getName() << endl;

Because it is not giving me "name" back but a blank.

Edited by saiz66
Link to comment
Share on other sites

  • 0
thank u both. it worked!

Is this how you use it?

S.setName("name");

cout << "Answer.Name = " << Answer.getName() << endl;

Because it is not giving me "name" back but a blank.

Yes, but isn't this line:

cout &lt;&lt; "Answer.Name = " &lt;&lt; Answer.getName() &lt;&lt; endl;

supposed to be:

cout &lt;&lt; "S.Name = " &lt;&lt; S.getName() &lt;&lt; endl;

bwx

Link to comment
Share on other sites

  • 0

oh i forgot to mention. I am inserting the S object into a index (hash table). And then putting all the information into another student object called Answer.

I know the index isn't the problem since it works with inetegers. Unless I am wrong. Any ideas?

Link to comment
Share on other sites

  • 0
oh i forgot to mention. I am inserting the S object into a index (hash table). And then putting all the information into another student object called Answer.

I know the index isn't the problem since it works with inetegers. Unless I am wrong. Any ideas?

I think you should post the full source code that is giving you the trouble so we can pinpoint the problem.

bwx

Link to comment
Share on other sites

  • 0

Sorry. Well I have the Index code from my professor. here is my main code:

Index&lt;Student, int&gt; I;

	Student S;
	Student Answer;

	S.setAge(2);

	S.setName("hi");

        cout &lt;&lt; "S.Name = " &lt;&lt; S.getName() &lt;&lt; endl;

	S.setID(5);

	cout &lt;&lt; "S.ID = " &lt;&lt; S.getID()&lt;&lt; endl;

        I.Retrieve(5,Answer);

	cout &lt;&lt; "Answer.ID = " &lt;&lt; Answer.getID() &lt;&lt; endl;
	cout &lt;&lt; "Answer.Age = " &lt;&lt; Answer.getAge() &lt;&lt; endl;
	cout &lt;&lt; "Answer.Name = " &lt;&lt; Answer.getName() &lt;&lt; endl;

I inserted S into the index with age, ID, and name. ID is the key for the hash table. And I tried to retrieve the key of value 5 which is ID of 5, and insert that into the object Answer. So it gave me back the right ID, right Age, but for Name it just put a blank. Any help please?

Link to comment
Share on other sites

  • 0

From the looks of your code, the only thing that comes to mind is that your copy constructor isn't copying the object correctly. Do you have a custom copy constructor for class Student?

Also, I didn't see any code inserting the S object into the Index, did you just omit it in the paste, or ... ?

bwx

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.