Full name with space??

This is a discussion on Full name with space?? within the C++ Programming forums, part of the General Programming Boards category; Hi, im writting a text game, and when the user inputs the name, I would like the whole name to ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,470

    Full name with space??

    Hi, im writting a text game, and when the user inputs the name, I would like the whole name to appear after it has been entered, like BILL BATES not just BILL.

    Do I still use cin >>??

    I have heard it has somthing to do with streams, but I am unsure on how to do this, as none of the tutorials have been any help on this subject

    thanks for the help in advance

  2. #2

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    Use the getline function:

    With char buffers:
    Code:
    char name[50];
    cin.getline(name,sizeof(name));
    Or with strings:
    Code:
    string name;
    getline(cin,name);
    I used to be an adventurer like you... then I took an arrow to the knee.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Do I still use cin >>??
    cin>> is programmed to read input until it encounters a whitespace character(spaces, tabs, newlines). So, it can't read in two names separated by a space all in one go.

    However, there is a funtion called getline(), which can read in a whole line of input. A line of input is deemed to end when getline() encounters a newline character in the input. A newline character is entered into the input when the user hits Enter after typing in the data. For instance, if the user types:

    Michael Jordan

    and then hits Enter, the input looks like this:

    Michael Jordan\n

    getline() will read everything up to and including the \n, and then getline() will discard the \n, so the input read into your string variable will be:

    Michael Jordan
    Last edited by 7stud; 06-15-2005 at 02:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Process memory space
    By barboris in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2008, 11:35 PM
  2. malloc allocates the same space in the memory
    By myle in forum C Programming
    Replies: 23
    Last Post: 11-20-2007, 06:52 PM
  3. Read space from the file
    By Ken JS in forum C++ Programming
    Replies: 9
    Last Post: 08-05-2007, 03:13 AM
  4. disk space mysteriously gone
    By evader in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2004, 12:30 PM
  5. ARGH! (argc and argv too!) HD Space!!!
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 12-05-2001, 03:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21