/* * @topic T00340 Apr 18 -- how to print HTML content withut tags v.2 * @brief Program demonstrates <tt>getline()</tt> function */ #include <iostream> #include <string> #include <sstream> #include <cctype> using namespace std; int main() { // This version of the program uses getline() // to get HTML input line-by-line. // HTML input is expected from std::cin string str_line; // placeholder for a line of HTML // Using memory buffer to store HTML stringstream buffer; while ( getline( cin, str_line) ) { // populate the buffer with each line of HTML buffer << str_line; } // Use unformatted input to read HTML character-by-character: bool flag_inside_tag = false; int onechar; // using int because char cannot represent EOF while ( ( onechar = buffer.get() ) != EOF ) { if ( onechar == '<') { // Encountered opening of an HTML tag flag_inside_tag = true; continue; } else if ( onechar == '>') { // Encountered closing of an HTML tag flag_inside_tag = false; cout << " "; continue; } else if ( flag_inside_tag == false ) { // Print everything outside of HTML tags std::cout.put( onechar ); } } return 0; }