I have a homework exercise where I need to create a filter that removes blank lines at the beginning and end of cin. Obviously the beginning is easy but I'm stumped on the end. I wanted to use a dynamic array but that's not allowed...
Here's the exercise text:
In this exercise develop a filter that removes empty lines existing at the beginning of cin and at the end of cin. A line is not empty if it contains at least one non-blank character. If an empty line contains blank characters then those characters can be ignored when writing that empty line to the standard output stream.
Caveats: we haven't covered arrays of undetermined sizes yet, so you can't store lines in an array, and you're not supposed to adopt the Perl approach, where you first read in the full file, and then process what you've got. Using a std::string variable is OK, but that variable may never contain more than one line read from the standard input.
I have absolutely no idea how you can know it's a blank line at the end without knowing what input is still to come. I'd appreciate any help or tips pointing me in the right direction.
I have a second, smaller, question. It's says file but does that mean it only need to work with an input file? As of now I can only make it work for either manual typing (ending input by some special combination of characters) or just scanning for a new line (which only works with files). Is there any way to end scanning the same way in both scenarios? (I'm just using a while loop atm)
And while we're at it, I'm kind of confused by the wording. Are lines containing only blank characters supposed to be printed, or treated as a blank line and skipped?
Thanks in advance!
Since this is homework, I will advise about the algorithm but not provide code (that would remove the point of the exercise).
Blank lines at the beginning and at the end must be removed, but blank lines in the middle must be kept. You can only cache one line of input. Think about it: do you need to cache an empty line to print it? Or can you print such a line (or many such lines) without caching them? And what necessarily follows after a block of one or more blank lines?
If these hints are not enough for you, here's the algorithm:
Read from
cinone line at a time. If it's not empty, print it. If it's empty, remember that you've encountered one blank line. Keep reading, and as long as successive lines are blank, keep incrementing the blank line counter. Eventually, you will either hit a non-empty lone, or EOF. If EOF, simply end. If a non-empty line, outputcounterempty lines, and resetcounter.
Regarding the other question: C++ abstracts files as streams of input (or output). A stream ends by reaching the EOF (end-of-file). std::cin is just one specific stream (subclassed from std::istream), as is std::ifstream. You also never know whether std::cin comes from the keyboard, or from a file (using e.g. shell redirection), and neither should you care. Simply write your code to the stream interface. Note that it is possible to send an EOF on interactive input too; in Linux for example, it's done by pressing Ctrl+D.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With