Thread: need help

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    9

    need help

    char *name,*nickname,*hobby;
    cout<<"Please enter your name:";
    cin>>name;
    cout<<"Please enter your nick name:";
    cin>>nickname;
    cout<<"Please enter your hobby:";
    cin>>hobby;

    anything wrong with this as the program will stop running?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    242
    i'm not sure exactly what the program is doing if you code it like that, but you haven't allocated any memory for the pointers (are they perhaps all pointing to NULL prior to being allocated?).

    you can either declare your variables that way, then have

    Code:
    name = new char[15];
    nickname = new char[15];
    hobby = new char[15];
    ...
    delete [] name;
    delete [] nickname;
    delete [] hobby;
    or else just start off with
    Code:
    char name[15];
    ...
    also, since c++ does no bounds checking (in case the user enters something bigger than the memory you've allocated), i'd use getline() rather than cin so that you can control what happens if the user enters a longwinded hobby, etc.
    getline - C++ Reference

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    They point to a random memory location. You should have a look at std::string class, dynamic allocation might confuse a beginner.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    erm, i actually creating a program that want user detail to generate password

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    can i know how to want the user to insert only words without numbers and vice verse?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You can't control what they type - you can however validate what they type after the event.

    So if your prompt says "letters and NO digits", then you check for that after the input has been made. If it does contain digits, then print an error message and try again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed