Another Array/Readin Question

This is a discussion on Another Array/Readin Question within the C++ Programming forums, part of the General Programming Boards category; Im usng cin, and trying to recogninze a newline character like this: Code: while( !cin.eof() ) { cin >> row ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    48

    Another Array/Readin Question

    Im usng cin, and trying to recogninze a newline character like this:

    Code:
    while( !cin.eof() ) {
       cin >> row >> col;
       if(col=='\n') {
          occupied = row;
          display_array(m1);
          init_array(m1);
       }
    }
    Is this possible? or would I have to use something like getline()?

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    i might be wrong..but i thought cin >> does not read any white spaces...(thats including the newline character...)....
    nextus, the samurai warrior

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I don't think that'll work but I'm not 100% sure. Try printing out the value of col after the cin and seeing what you get with known input.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by nextus
    i might be wrong..but i thought cin >> does not read any white spaces...(thats including the newline character...)....
    You are correct. To actually read in a newline, you would probably use cin.get(), or maybe getline with a different delimiter.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    doesnt cin.get() only work with char data types?

    I am working with integers... reading integers...
    Last edited by guda; 11-24-2004 at 12:28 PM.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Why are you checking for the newline? That is the real question. What are you trying to do with that?

    Maybe give an example of your input so we can see what cases you are trying to handle.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    OK, this isnt a project, just a sample test that we are suppose to work through, however the "real test" will closely resemble it.. ie, reading in, processing double matrix..


    The first Number "2" in the example input specifies how many Matrices are in the file, whereas the Second Number "3' specifies the Number of seats occupied, and the Third Row specifies the "matrices size".


    Code:
    For example:
    1 one chart
    
    Example Input:
    2
    3
    2 3
    0 0
    1 1
    1 2
    3
    3 3
    0 0
    1 1
    2 2
    
    Example Output:
    XEE
    EXX
    
    XEE
    EXE
    EEX
    
    The first chart:
    3    three seats occupied
    2 3  two rows three columns
    0 0  seat row 0 column 0 occupied
    1 1  seat row 1 column 1 occupied
    1 2  seat row 1 column 2 occupied
    
    The second chart:
    3    three seats occupied
    3 3  three rows and three columns
    0 0  seat row 0 column 0 occupied
    1 1  seat row 1 column 1 occupied
    2 2  seat row 2 column 2 occupied
    I'm using the "\n" to determine when there is only "one" number to be read in, since that would signify the beginning of a "new matrix"..

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    int peek ( );
    Peek next character.
    Reads and returns the next character without extracting it from the stream.
    http://www.cplusplus.com/ref/iostream/istream/peek.html
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    As long as you're using >> with single characters (but no other type works), you can also use the noskipws manipulator to make the stream extract whitespace as characters.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    got it... thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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