Thread: string object problem

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    string object problem

    hello guys,
    i got small problem with my code can any check that cout for me please.

    this is skeleton of my structure
    Code:
    struct emp
    {
        string type;
        int empid;
        string surname;
        string firstname;
    };
    
    struct manag
    {
        struct emp details;
        struct manag *left;
        int empno;
        int *empid;
        struct manag *right;
        struct tech *tec;
        struct admin *admi;
    };
    and this is one of the fucntion where actaully i an using this struct
    Code:
    void inserttree(string str)
    {
        char *p,*q;
        
        
        p=(char *)str.c_str();
        
        if((q=strtok(p," "))!=NULL)
        {
            if(strcmp(p,"manager")==0)
            {
                struct manag *manag;
                manag->details.type += q;
                cout<<manag->details.type<<endl;
                cin.get();
        }
                
        }
    }
    does any one find anything wrong with this code. i am just getting the first work from the string and copy that string into my struct. but when i run this code it just give an runtime error. i belive that its soemhting to do with the string which i am copying. can any one check that out for me please

    ssharihs2005

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > p=(char *)str.c_str();
    c_str() is supposed to be a const char *, so removing the const with a cast, and then trashing the string using strtok() is generally a bad idea.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Yeah strtok() is evil it modifies the passed string.
    You cast away the constness of string.c_str(). It gets modified ->boom
    Kurt

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    right thax very much, so is there any better way of doing that and i think it's better to go with the normal string char *

    ssharish2005
    Last edited by ssharish2005; 12-18-2005 at 06:25 AM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    >> so is there any better way of doing that and i think it's better to go with the normal string char *
    shure there is. use a stringstream e.g.
    Code:
    void inserttree(string str)
    {
        stringstream sstr(str);
        string manager;
        sstr >> manager;
        if ( manager == "manager" ) {
           string add;
           sstr >> add; // if add can contain spaces you could use getline()
           struct manag *manag; // you should allocate a manag struct here
           manag->details.type += add;     // this I don't understand
           cout<<manag->details.type<<endl;
           cin.get(); // ???    
        }
        // ....
    }
    Kurt

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    thax ZuK for the solution. i got this problem check out this

    Code:
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    struct emp
    {
        char type[25];
        int empid;
        char surname[25];
        char firstname[25];
    };
    
    struct manag
    {
        struct manag *lef;
        struct manag *right;
        struct tech *tec;
        struct admin *admi;
        struct emp details;
        int empno;
        int *empid;
        
    };
    
    int main()
    {
        struct manag *man;
        char *p="helloword";
        
        strcpy(man->details.type,p);
        
        cout<<man->details.type<<endl;
        
        cin.get();
    }
    why dons't this print me the string. it gives Seg Fault.

    ssharish2005

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You don't allocate anything for man
    Code:
    int main() {
        // struct manag *man;     // C++ doesn't need struct here
        manag *man = new manag;   // allocate
        char *p="helloword";
        
        // strcpy(man->details.type,p); // wrong man->details.type is a std::string
        man->details.type=p;            // there is an assignement operator for string
        cout<<man->details.type<<endl;
        
        cin.get();
    }
    This should work

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by ZuK
    man->details.type=p; // there is an assignement operator for string
    well, this dont work Zuk as i have declared a type as char string thats is
    Code:
    char type[25];
    when i do it like what u say i will an error like incompatiable assigment.

    ssharish2005

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    not to worry i solved the problem, any way thax for all your help

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. string object to null-terminating string conversion
    By sayz0 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2001, 12:15 PM