#include <iostream>
#include <sstream>
int main()
{
std::string s = "5 A B C A D E\n10 G E A F G B C A E F E\n 3 A C B D\n";
std::istringstream is( s );
unsignedint count;
while ( is >> count )
{
char sentinel;
is >> sentinel;
bool ignore = false;
while ( count-- )
{
char c;
is >> c;
if ( !ignore && !( ignore = c == sentinel ) ) std::cout << c << ' ';
}
std::cout << std::endl;
}
}
I mean like, how every line works or how the program goes.
I don't know enough to explain exactly how everything works. If the following is not enough, ask for an explanation of a specific line and I will attempt to respond with a better answer. Or ask a more knowledgeable member, like Vlad from moscow.
#include <iostream>
#include <sstream>
int main()
{
// Definition of std::string object that will be read.
// http://en.cppreference.com/w/cpp/string/basic_string/basic_string
std::string s = "5 A B C A D E\n10 G E A F G B C A E F E\n 3 A C B D\n";
// Definition of std::istringstream object that contains the previously defined string.
// http://en.cppreference.com/w/cpp/io/basic_istringstream/basic_istringstream
std::istringstream is( s );
// Declaration of variable that will later store the amount
// of characters that need to be extracted from the string stream.
unsignedint count;
// Continue executing the contents of the while loop's compound statement until
// std::istringstream::operator bool() returns false
// http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool
while ( is >> count )
{
char sentinel;
is >> sentinel;
// Extract data from the stream and store in specified variable (sentinel).
// http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt
bool ignore = false;
// Decrement count variable by one and then return the value prior to decrementation.
// If value of count is zero, the condition is false and the loop ends.
// Else, continue execution of the loop!
while ( count-- )
{
char c;
is >> c;
// Extract data from the stream and store in the specified variable (c).
// http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt
if ( !ignore && !( ignore = c == sentinel ) )
std::cout << c << ' ';
// If the expression evaluates to true (ignore is false and the resulting value
// after assignment is false), write c and a space to standard ouput stream.
}
std::cout << std::endl;
// Flush the output buffer and write a newline character to the standard output stream.
// http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt
// http://en.cppreference.com/w/cpp/io/manip/endl
}
}
As it was said in your assignment you should output only letters that precede the sentinel letter. All other letters are simply ignored. This statement
if ( !ignore && !( ignore = c == sentinel ) ) std::cout << c << ' ';
checks whether the sentinel was encountered and whether the current letter that was read is the sentinel letter. If not so then the letter is printed.
This is the line that I don't understand, I'm confused on what happens here.
if ( !ignore && !( ignore = c == sentinel ) ) std::cout << c << ' ';
The preceding statement will be executed if the condition of the if statement evaluates to true. So, the data will be wrote if 'ignore' negated is true. In other words, the variable 'ignore' must hold the value false before negation in order for the expression to evaluate to true. This applies to both operands of the logical and operator.
The result of the not operator followed by 'ignore' is equivalent to 'ignore == false' in this case.
!( ignore = c == sentinel )
The new value of a variable of a primitive type will be returned after assignment. If 'c' is equal to 'sentinel', then 'ignore' is assigned to true. Otherwise, 'ignore' is assigned to false. The result of this assignment is then negated.
Like I said, I am a beginner, a learning student, in the process of learning things. And I am thankful for the code you gave me, really I am. I was only asking for help on understanding some stuff better. I will no longer bother you with it, I'll just try to do it myself, thanks again.