943,714 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3258
  • C++ RSS
Jan 11th, 2009
0

How do I put a char array in a class???

Expand Post »
So I'm having trouble with classes today, I want to do something like

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. using namespace std;
  3. class Dummy{
  4. char Name[256];
  5. void setName(char nam){Name=nam;}
  6. };
  7.  
  8. int main(){
  9. char[256] temp;
  10. cout << "Name?\n";
  11. cin >> temp;
  12. setName(temp);
  13. }

I want something like this to store a char array in a class, but it never works, I've googled all this stuff and just cant find it, Help!
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
Manutebecker is offline Offline
51 posts
since May 2008
Jan 11th, 2009
0

Re: How do I put a char array in a class???

c++ Syntax (Toggle Plain Text)
  1. Dummy dum;
  2. dum.setName(temp);
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jan 11th, 2009
0

Re: How do I put a char array in a class???

Try this. Comments added explaining the reason for change from your code.
#include <iostream>      // <iostream> is standard, <iostream.h> is not
#include <cstring>        //   need functions to work with C strings

using namespace std;
class Dummy{
char Name[256];

public:    // for main() to use a member function, it must be public
void setName(char *nam)       // arrays are passed to function as pointers
    {strcpy(Name, nam);}   // and char arrays are copied using string functions like strcpy()
};

int main(){
char temp[256];           //   the declaration char [256] temp is invalid
cout << "Name?\n";
cin >>  temp;
Dummy some_object;   //  non-static member functions act on objects
some_object.setName(temp);  // this is how non-static member functions act on objects
}

You also need to put some effort into interpreting and understanding error messages from your compiler. Your compiler would have complained bitterly about your code and, if you'd taken time to understand them, you could worked out the changes I've commented in red.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Jan 11th, 2009
0

Re: How do I put a char array in a class???

yeah I actually had it on a seperate pc and didnt ctrl v it onto here, rather quickly interpreted it, my bad, but thx a lot, and one last thing, I tried to add a function called
C++ Syntax (Toggle Plain Text)
  1. char getName(){return *Name;}

However it only returns the first letter, god I fail
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
Manutebecker is offline Offline
51 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with external errors!!!
Next Thread in C++ Forum Timeline: does debug symbols in code slowdown program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC