[RESOLVED] *char user input
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] *char user input

  1. #1
    Join Date
    Apr 2013
    Posts
    34

    [RESOLVED] *char user input

    gragh! this should be simple! >.< I thought I had a good understanding of this by now.. guess I don't

    basicly I want to do this:

    Code:
    char * name = "bob";
    only have the user input it.

    something like:

    Code:
    char * name;
    cin >> name; //only this doesn't work
    I must have tried a million different variations, different codes, jumping from different pointers, converting from strings, specifying specific char lengths, you name it, yet I either get an "not initialized" error (though isn't the cin initializing it? even so, even if I initialize it before the cin, I still get that error) or I get the pointer to the name the user entered, and the code I borrowed off someone else doesn't seem to take that because.. they seem they want the pointer to be the char, not a pointer to the char :s all I know is that the first example works beautifully, but as soon as I try to get the char from the user input it goes all poopy.

    any help?

    this should be simple in my head..

  2. #2
    Join Date
    Jun 2013
    Posts
    21

    Re: *char user input

    Try this

    Code:
    char name[256];
    
    cin >> name;

  3. #3
    Join Date
    Apr 2013
    Posts
    34

    Re: *char user input

    ??

    yes it worked perfectly! thank you

    though I could have sworn that was one of the first things I tried :s... weird. but thank you!

  4. #4
    Join Date
    Jun 2012
    Posts
    38

    Re: [RESOLVED] *char user input

    Hi P&P, that only works if you input contains no space

    try this
    char name[256];
    cin.getline(name,strlen(name)+1);

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,389

    Re: [RESOLVED] *char user input

    Quote Originally Posted by terminalXXX View Post
    Hi P&P, that only works if you input contains no space

    try this
    char name[256];
    cin.getline(name,strlen(name)+1);
    NO! name has not been initialised so no assumptions can be made about its content. strlen(name) returns the number of characters (unknown!) in the uninitialised char array. It is totally unpredictable how many characters can actually be input using this. It's possible that the char name array can overflow causing memory corruption problems.

    If you really want to use char array (as opposed to c++ string) then the way to code this is:

    Code:
    const int MAX_INPUT = 255;
    char name[MAX_INPUT + 1] = {0};
    cin.getline(name, MAX_INPUT + 1);
    It would be better to use c++ string and use

    Code:
    string uinp;
    getline(cin, uinp);
    Last edited by 2kaud; June 29th, 2013 at 07:58 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center