little question!

Discussion in 'C++' started by creative, May 29, 2010.

  1. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    now I am working with c + +. So I'm even a small little program written
    Code:
    #include <iostream.h> 
    #include <string> 
    
    
    class Spieler 
    { 
    public: 
        Spieler() 
        { 
            name = new char[]; 
        } 
        char *name; 
    }; 
    
    
    
    int main() 
    { 
        Spieler player1; 
        cout << "Bitte Namen eingeben\n"; 
        cin >> player1.name; 
        cout << player1.name; 
        
        return 0; 
    
    }
    works, too. Only if I enter too many characters, gives irgendso ne bad message. Is there ne better?
     
  2. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    class player
    (
    public:
    std:: string name;
    );
     
  3. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    I've already tried, but then reports the following:

    C: \ Program Files \ Microsoft Visual Studio \ MyProjects \ Classes \ klassenmain.cpp (17): error C2679: binary '>>': no operator defined which takes a right-hand operand of type 'class std:: basic_string <char,struct std::char_traits<char> , Class std:: allocator <ch
    ar>> '(or there is no acceptable conversion)
    C: \ Program Files \ Microsoft Visual Studio \ MyProjects \ Classes \ klassenmain.cpp (18): error C2679: binary '<<': no operator defined which takes a right-hand operand of type 'class std:: basic_string <char,struct std::char_traits<char> , Class std:: allocator <ch
    ar>> '(or there is no acceptable conversion)
     
  4. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    std:: getline (cin, player1.name);
     
  5. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    # Include <iostream>

    using namespace std;
     
  6. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    http://ipankaj.net
    Code:
    name = new char [];
    This is cheese. You must specify a size already.

    The version with std:: string is much better.

    To your problem:
    Make the <iostream.h> a <iostream> and you should be rid of the problem.
     
  7. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    Ok, thank you for the quick answers ...

    But I Could you perhaps explain a few things:

    My code now looks like this:
    Code:
    #include <iostream> 
    #include <string> 
    
    using namespace std; 
    
    class Spieler 
    { 
    public: 
        std::string name; 
    }; 
    
    
    
    int main() 
    { 
        Spieler player1; 
        cout << "Bitte Namen eingeben\n"; 
        player1.name = "HALLO"; 
        cout << player1.name; 
        
        return 0; 
    
    }  
    What is the purpose of using namespace std ?????
     
  8. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    if you with "new char [x]" want to leave it, then you still lack
    a "delete [] name" in dtor of player.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice