Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok. Lets go over you code by parts to make things clear.</p> <pre><code>char filter = " " // &lt;&lt;== this is a tab space, so I am assuming its one char </code></pre> <p>Your first statement creates a variable of type char. The type char in c++ take a value, like any other variable. The only difference is that if you try to use it with a statement, like cout, it will be interpreted as a character by it's ASCII value. What you did compiles and works, but the char you declared has the value that c++ gives for the string " ". The value for the horizontal tab (TAB) in ASCII is 9.</p> <p>So, for declaring a char of datatype char with the value for a TAB:</p> <pre><code>char filter = 9; </code></pre> <p>But you don't have to keep remembering all those values. The same way C++ will give a value for the string " ", it will give a value for a character. If you want to get the value for characters, you have to use ''. A confusion that some make is to assume that tab is actually a group of spaces. Tabs are interpreted, by a text editor, as a group of spaces. But it is in fact a single character. In C++, the character for the tab is \t. If you actually put this inside a string and call cout, you will have a tab. The reason why \t is a single character and not two characters is because the compiler gives "special" interpretations for the immediate character after a backslash inside '' or "".</p> <p>So the following will give the value of 9 to a variable of datatype char:</p> <pre><code>char filter = '\t'; </code></pre> <p>Now for your if statement:</p> <pre><code>if ( line[counter] == filter[0]) </code></pre> <p>Your if statement is comparing the character with index "counter" in the string line. This is possible because a string is an array of characters (It is actually a class, but don't worry about it for now). But filter is not an array, it is a single character. So it wont have an index (E.g. [0]). All you need is the variable you created. This confusion usually happens because the concept of string is often introduced without explaining that a string is not a datatype (That's why is not blue, and that's why you have to include a library).</p> <p>So, just removing the index you will have:</p> <pre><code>if (line[counter] == filter) </code></pre> <p>You could, of course, just compare it to a tab directly</p> <pre><code>if (line[counter] == 9); </code></pre> <p>or</p> <pre><code>if (line[counter] == '\t'); </code></pre> <p>Hope this gives you a better understanding of what is actually happening.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload