Thread: Reading input from keyboard

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    Reading input from keyboard

    I have to read in an undetermined amount of 5 character strings from the keyboard, all entered in one line. Valid input for example, with each example on a new line:

    Code:
    BBBBG YPZAA ABCD6
    BBBBG WANAB REWWW ZZAA0 PPP14
    AAAAC BBBBE CCCC5 ABCD4 QQQ3R ERFG8 
    AAAAB CCC14 QQQQD ZZZZ4 ZE_B12 
    AAAAC BBBBE CCCC5 ABCD4 QQQ3R ERFG8 PPP14 REWD2
    I need to get each 5 character string from the input and store them all into an array. I do not know how to do this.

    For testing I just faked it, using "cin >>" five times. I need to expand tha to accept as few as 1 and as many as 20.

    Thanks for your time and help.

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    look up members of cin. google is a great place to search MSDN quickly. I believe cin.getline might do what you want, however, I forgot how to only get certain characters. cin.ignore maybe.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    You can use fgets()

    The fgets function reads a string from the input stream argument and stores it in string. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    fgets() is the C way to do it. getline() is the C++ way.

    You could just read in 5 char variables if the input is always like that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    I just forgot that i am using c++ forum.....

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need to combine getline with operator>>. First use getline to read the line into a string. Then put that string into a stringstream and read from the stringstream into the spots in the array until the stringstream is empty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using threads for control input (keyboard, serial)
    By synthetix in forum C Programming
    Replies: 1
    Last Post: 07-06-2009, 07:43 PM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Reading from an input file + processing
    By Mooncow in forum C Programming
    Replies: 2
    Last Post: 12-01-2008, 02:45 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Help with linked list (and reading user input)
    By p1kn1c in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:43 AM