Need help with using strings in classes

This is a discussion on Need help with using strings in classes within the C++ Programming forums, part of the General Programming Boards category; I need help getting strings to work with my class. Integers, floats etc all seem to work, but strings won't. ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    19

    Need help with using strings in classes

    I need help getting strings to work with my class. Integers, floats etc all seem to work, but strings won't. I think I need to use a pointer but I'm a noob and can't figure out how to do that.

    Here's the code
    Code:
    #include<stdlib.h>
    #include<iostream>
    #include<string>
    using namespace std;
    
    
    class object
            {
            private:
                    char name[20];
                    char description[80];   // Longer description
                    int where;              // Where object is, -1 if carried
            public:
                    void setup(char n, char d, int w);
                    void desc(int room);
            };
    
    void object::setup(char n, char d, int w)
            {
            strcpy(name[20], n);
            strcpy(description[100], d);
            where = w;
            }
    
    void object::desc(int room)
            {
            if ((room == where) || (where == -1))
                    {
                    cout << "The cheese is "<< description << endl;
                    }
            else
                    {
                    cout << "I can't find the " << name << ", can you?" << endl;
                    }
            }
    
    int main()
            {
            object cheese;
            cheese.setup("Swiss cheese","holey and yellow",1);
            cheese.desc(1);
            cheese.desc(2);
            }
    and here are the errors:

    Code:
    object.cpp: In member function `void object::setup(char, char, int)':
    object.cpp:20: error: invalid conversion from `char' to `char*'
    object.cpp:20: error: invalid conversion from `char' to `const char*'
    object.cpp:21: error: invalid conversion from `char' to `char*'
    object.cpp:21: error: invalid conversion from `char' to `const char*'
    object.cpp: In function `int main()':
    object.cpp:40: error: invalid conversion from `const char*' to `char'
    object.cpp:40: error:   initializing argument 1 of `void object::setup(char,
       char, int)'
    object.cpp:40: error: invalid conversion from `const char*' to `char'
    object.cpp:40: error:   initializing argument 2 of `void object::setup(char,
       char, int)'

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You already include the file <string>. Use the std::string class contained within, it's far easier to use than raw strings.

    Regarding your errors:
    Code:
    void object::setup(const char *n, const char *d, int w)
            {
            strcpy(name, n);
            strcpy(description, d);
            where = w;
            }
    But be aware that this is an invitation for memory corruption: if the strings you pass are longer than what the class can hold (19 characters for name, 99 for description), then it will overwrite data it shouldn't. You can use strncpy to prevent that, however strncpy isn't yet a C++ standard function (it's in the 1999 revision of the C standard).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    thanks for the help, but that didn't seem to work. though I did make a few changes and got rid of most of the errors.
    btw im not sure how to use the std::string class

    here's the updated code
    Code:
    #include<stdlib.h>
    #include<iostream>
    #include<string>
    using namespace std;
    
    
    class object
            {
            private:
                    char name[20];
                    char description[80];   // Longer description
                    int where;              // Where object is, -1 if carried
            public:
                    void setup(char n[20], char d[80], int w);
                    void desc(int room);
            };
    
    void object::setup(char n[20], char d[80], int w)
            {
            strcpy(name[20], n);
            strcpy(description[100], d);
            where = w;
            }
    
    void object::desc(int room)
            {
            if ((room == where) || (where == -1))
                    {
                    cout << "The cheese is "<< description << endl;
                    }
            else
                    {
                    cout << "I can't find the " << name << ", can you?" << endl;
                    }
            }
    
    int main()
            {
            object cheese;
            cheese.setup("Swiss cheese","holey and yellow",1);
            cheese.desc(1);
            cheese.desc(2);
            }
    and the errors:

    Code:
    object.cpp: In member function `void object::setup(char*, char*, int)':
    object.cpp:20: error: invalid conversion from `char' to `char*'
    object.cpp:21: error: invalid conversion from `char' to `char*'

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I showed you the correct code. Why did you change it?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    sorry, I was still getting alot of errors,
    Here's what I had with that code

    Code:
    object.cpp: In member function `void object::setup(const char**, const char**,
       int)':
    object.cpp:20: error: invalid conversion from `char' to `char*'
    object.cpp:20: error: cannot convert `const char**' to `const char*' for
       argument `2' to `char* strcpy(char*, const char*)'
    object.cpp:21: error: invalid conversion from `char' to `char*'
    object.cpp:21: error: cannot convert `const char**' to `const char*' for
       argument `2' to `char* strcpy(char*, const char*)'
    object.cpp: In function `int main()':
    object.cpp:40: error: no matching function for call to `object::setup(const
       char[13], const char[17], int)'
    object.cpp:19: error: candidates are: void object::setup(const char**, const
       char**, int)

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    ah i figured it out i just had to delete the array index beside the name and description in the updated code. Thanks!
    Last edited by orikon; 11-12-2005 at 02:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  3. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 05:49 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21