Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

spetnaz_

here is a challenge for the C++ experts

This topic is 5480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have been observing nehe for few months now and well this is the best dev forum out there so here goes. I have been trying to develop a little win 32 console database. Its just so I could remember all the C++ after months of not programming, you know revising for exams and so on. Basically, I would like to ask some of the C++ experts out there to help me with my problem. I declare an Employee class and in the constructor parameters I put “char name[9]”. I also declare a private “char m_Name[9]” array. Then later when defining the constructor I have: for (int i = 0; i =< 9 ; i++) { m_Name = name[i]; } so when I create an object I type in this (the constructor has other perameters like “int age”): Employee Greg (24,45,23,Greg). When I compile I get error messages, which are way too cryptic for me to work out why this doesn’t work. Without the name array bit everything work perfectly. Also I have accessor member functions “void SetName(char name[9])” and “char GetName()” which work on the same kind of for loops I have descried. Can some one please tell me why it does not compile? On the a accessor front I think the output type for “GetName()” might be wrong since it is char and probably should be char[9]. In addition can you actually have an output of a function as an array, because if there is not I just wasted 2 hours. thank you for your kind replies

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
One word to say...

strncpy

ZixThree

Share this post


Link to post
Share on other sites
The reason it''s not working is you are overwriting the array bounds. Your for loop should read:
for(int i = 0; i < 9 ;++i)

not i =< 9.

Also for your GetName function just declare the return type as
const char *.

Since you are using c++, you may be better using std::string, since this would allow you to do m_name = name, and it would allow you to have any length(within reason) names

Share this post


Link to post
Share on other sites
Employee Greg (24,45,23,Greg);
//Is this copyed directly from youre code ? well it`s wrong,
you decalre himself ? like -> int A[]={A,A,A}; //?
it should be at least :
Employee Greg (24,45,23,"Greg");

Share this post


Link to post
Share on other sites
Employee Greg (24,45,23,Greg);
//Is this copyed directly from youre code ? well it`s wrong,
you declare himself ? like -> int A[]={A,A,A}; //?
it should be at least :
Employee Greg (24,45,23,"Greg");

Share this post


Link to post
Share on other sites
thanx for the quick reply, ill try out the "Greg" suggestion by cippyboy, later. jamessharpe i know that was wrong thanx for pointing it out, but that is not what the code looks like, in the code it is "=<9", and also for the output as a pointer, doesnt it actually have to point at the same data type so char* can only be used on char and not on an array.


Anonymous Poster, i think you have actually hit the bulls eye. I will check that later latter.

clippboy can you plz explain what "you decalre himself ? like -> int A[]={A,A,A}; //?" means, because thats how u load an array in the code not at run time, and i am trying to do it at run time, thanx anyway.


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
int A[] = {A, A, A}

Meaning:

when you declare this array, you''ll actually have an infinite loop of declarations.

A
/ | \
A A A
/|\/|\/|\
AAA AAA AAA

And so on...

This will definetly cause a compiler error.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
easy, what you have to do is :

m_Name = name[i];

what you wrote is wrong, because m_Name is the adresse of the char array, so you try to replace an adresse with a character.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
is it an error from the forum editor or what ?

what you have to write is

m_Name = name[i];

(in case what I wrote shouldn''t be displayed correctly, you have to type ''['' ''i'' '']'' after m_Name

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!