need help with cin.get, or cin.ignore

This is a discussion on need help with cin.get, or cin.ignore within the C++ Programming forums, part of the General Programming Boards category; I need help with a class project, I need it to be functions and I really don't understand how to ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    2

    Unhappy need help with cin.get, or cin.ignore

    I need help with a class project, I need it to be functions and I really don't understand how to use cin.ignore and cin.get with an input file, but here goes the program is going to process a text file, I pretty much understand that part, and output the data as is so that is easy. The part that I am having trouble with is how to process blanks or white spaces so the the output file will count the number of words on a line the number of lines and the number of paragraphs. I understand the it should read the characters and skip them if they are not blanks so that it can count the words. The extraction operator >> skips blanks so I know that I need to use cin.ignore I would think so that when it comes to a character it will ignore it and read only the blanks so that it can count the words, but how would it count the lines and paragraphs? I also know that I need a counter. so can someone give me some guidence on how to use cin.ignore so it can ignore lines with charaters on it to count the number of lines in a paragraph??? thanks in advance

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Just to give you some hints.
    It's a newline after you encounter '\n'
    and if the text is like this...

    xxxxxxxxxxxxx xxxxxxxxxxx xxx xxxxx xx
    xxxxx xxxxxxxxxx xxxxxxxx xxxxxx
    ----->xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxx xxxxxxxxx xxxxxxx
    xxxxxxx

    then a a paragraph is when you encounter " " after '\n'
    That's one way, the easiest that I could think of for newbee

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
    There are several things in "How do I...level 2" that might be of help
    Away.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    2
    Originally posted by alphaoide
    Just to give you some hints.
    It's a newline after you encounter '\n'
    and if the text is like this...

    xxxxxxxxxxxxx xxxxxxxxxxx xxx xxxxx xx
    xxxxx xxxxxxxxxx xxxxxxxx xxxxxx
    ----->xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxx xxxxxxxxx xxxxxxx
    xxxxxxx

    then a a paragraph is when you encounter " " after '\n'
    That's one way, the easiest that I could think of for newbee

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >> ignores leading white space but doesn't ignore terminating whitespace. Any whitespace char (space, tab, carraige return, newline char, etc) will terminate input. Therefore, if you just use >> you will not be able to tell what caused termination. Since >> leaves the terminating char in the input buffer, you can figure it out, but.....then you might as well read char by char in the first place. That way you can look at the next char once you find a target char. Target char may be spaces (there are routinely two spaces after the last word of a sentence and before the first word of the next sentence) or new line char(if the next char after the new line char is a space followed by a new line char or a new line char itself, then you have (probably) found a new paragraph and not a new line.

  6. #6
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Hi yoyo,

    Your algorithm will be useless if you type the file, you don't use any full stops or paragraphs! :P
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.ignore and cin.get in C
    By Trafalgar Law in forum C Programming
    Replies: 5
    Last Post: 11-24-2008, 10:34 AM
  2. cin.get and cin.ignore
    By Trafalgar Law in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2008, 10:49 AM
  3. cin.get(); doesn't work even with cin.ignore();
    By calumn in forum C++ Programming
    Replies: 34
    Last Post: 05-14-2006, 12:12 PM
  4. cin.ignore() & cin.get()
    By dragonlady in forum C++ Programming
    Replies: 11
    Last Post: 08-09-2005, 12:38 AM
  5. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 12:44 PM

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