| | |
How do I put a char array in a class???
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
So I'm having trouble with classes today, I want to do something like
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!
C++ Syntax (Toggle Plain Text)
#include <iostream.h> using namespace std; class Dummy{ char Name[256]; void setName(char nam){Name=nam;} }; int main(){ char[256] temp; cout << "Name?\n"; cin >> temp; setName(temp); }
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!
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
Try this. Comments added explaining the reason for change from your code.
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.
#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.
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
However it only returns the first letter, god I fail
C++ Syntax (Toggle Plain Text)
char getName(){return *Name;}
However it only returns the first letter, god I fail
![]() |
Similar Threads
- Safe Array (C++)
- fstream Tutorial (C++)
- Problem declaring class object array (C++)
- a class problem (C++)
- Class Vectorization requirements (C++)
- Array troubles? (C++)
- Someone help me hate trees less (Java)
- Sorting character arrays! (C++)
- can a vector take array of char(char *) (ASP.NET)
- Need some help with a Java lab... (Java)
Other Threads in the C++ Forum
- Previous Thread: Help with external errors!!!
- Next Thread: does debug symbols in code slowdown program
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






