CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured