Results 1 to 4 of 4

Thread: reading text into a character array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    WestCoast - Cali
    Posts
    116

    reading text into a character array

    How can you read in a name of a person into a character array without entering a single letter at a time?

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I didn't get it.
    Can't you just use cin with the array name?
    PHP Code:
    char name[20];
    cin>>name
    Baaaaaaaaah

  3. #3
    Member
    Join Date
    Jul 2002
    Location
    Singapore
    Posts
    32
    Example :

    char name[20];
    cin.get(name, 20, '\n');
    cin.ignore(80,'\n');

    what this does is get 19 characters from the stream or until it encounters the \n character, whichever comes first. Since the delimiter is left in the stream, cin.ignore() will ignore 80 chars or up to the \n

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Or why not be safer for people with long names?
    Code:
    string name;
    getline(cin, name);
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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