Ignore a specific letter and then output the next letters after it.

aaz.
Last edited on
Try to substitute in this program std::istringstream for std::ifstream yourself

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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 );

	unsigned int 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;
	}
}
Last edited on
That was only an example file though, what if the file was edited and the text in it was changed?
Do you have not understood? One more substitute in this program std::istringstream for std::ifstream yourself
Oh I get it, simply read the code earlier. I'll have to study the code myself then, thanks!
Hmm, would you mind explaining your code? Can't seem to think over it lol sorry.
Its explanation is in the description of your assignment.
I mean like, how every line works or how the program goes.
closed account (2b5z8vqX)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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.
	unsigned int 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
	}
}

Last edited on
You will learn much better, if you try to explain each line by yourself. You must understand some of them already. Explain those for us.
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 codes above it I get it now. So count gets 5, sentinel gets A. And while count is decreasing c gets the letters after A and outputs it.
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.
Last edited on
closed account (2b5z8vqX)
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.

http://en.cppreference.com/w/cpp/language/operator_logical

(My explanation is messy and perhaps incorrect but I will post it anyway. If you still don't understand, then ask another member.)
Last edited on
So uh ignore = c == sentinel, if the letter assigned to c is the same as the letter assigned to sentinel, therefore ignore becomes true?
Rewrite the expression using parentheses that it would be more clear

ignore = ( c == sentinel );
Last edited on
I don't understand if ignore becomes true or a value is assigned to it... And what is !ignore for? Uhh, sorry guys...
BrotherFromAnotherMother


Switch on your brain. Here is no kindergarten. If you do not understand the code I showed then write code yourself. It is your assignment.
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.
Topic archived. No new replies allowed.