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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 51
Reputation: Manutebecker is an unknown quantity at this point 
Solved Threads: 2
Manutebecker's Avatar
Manutebecker Manutebecker is offline Offline
Junior Poster in Training

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

 
0
  #1
Jan 11th, 2009
So I'm having trouble with classes today, I want to do something like

  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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

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

 
0
  #2
Jan 11th, 2009
  1. Dummy dum;
  2. dum.setName(temp);
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

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

 
0
  #3
Jan 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 51
Reputation: Manutebecker is an unknown quantity at this point 
Solved Threads: 2
Manutebecker's Avatar
Manutebecker Manutebecker is offline Offline
Junior Poster in Training

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

 
0
  #4
Jan 11th, 2009
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
  1. char getName(){return *Name;}

However it only returns the first letter, god I fail
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC