Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing Character Literal to Std::String in C++
    primarykey
    data
    text
    <p>I would like to compare a character literal with the first element of string, to check for comments in a file. Why use a char? I want to make this into a function, which accepts a character var for the comment. I don't want to allow a string because I want to limit it to a single character in length.</p> <p>With that in mind I assumed the easy way to go would be to address the character and pass it to the std::string's compare function. However this is giving me unintended results.</p> <p>My code is as follows:</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; int main ( int argc, char *argv[] ) { std::string my_string = "bob"; char my_char1 = 'a'; char my_char2 = 'b'; std::cout &lt;&lt; "STRING : " &lt;&lt; my_string.substr(0,1) &lt;&lt; std::endl &lt;&lt; "CHAR : " &lt;&lt; my_char1 &lt;&lt; std::endl; if (my_string.substr(0,1).compare(&amp;my_char1)==0) std::cout &lt;&lt; "WOW!" &lt;&lt; std::endl; else std::cout &lt;&lt; "NOPE..." &lt;&lt; std::endl; std::cout &lt;&lt; "STRING : " &lt;&lt; my_string.substr(0,1) &lt;&lt; std::endl &lt;&lt; "CHAR : " &lt;&lt; my_char2 &lt;&lt; std::endl; if (my_string.substr(0,1).compare(&amp;my_char2)==0) std::cout &lt;&lt; "WOW!" &lt;&lt; std::endl; else std::cout &lt;&lt; "NOPE..." &lt;&lt; std::endl; std::cout &lt;&lt; "STRING : " &lt;&lt; my_string &lt;&lt; std::endl &lt;&lt; "STRING 2 : " &lt;&lt; "bob" &lt;&lt; std::endl; if (my_string.compare("bob")==0) std::cout &lt;&lt; "WOW!" &lt;&lt; std::endl; else std::cout &lt;&lt; "NOPE..." &lt;&lt; std::endl; } </code></pre> <p>Gives me...</p> <pre><code>STRING : b CHAR : a NOPE... STRING : b CHAR : b NOPE... STRING : bob STRING 2 : bob WOW! </code></pre> <p>Why does the function think the sub-string and character aren't the same. What's the shortest way to properly compare chars and std::string vars?</p> <p>(a short rant to <strong>avoid reclassification</strong> of my question.... feel free to skip)<br> When I say shortest I mean that out of a desire for coding eloquence. Please note, this is NOT a homework question. I am a chemical engineering Ph.D candidate and am coding as part of independent research. One of my last questions was reclassified as "homework" by user <a href="https://stackoverflow.com/users/282912/msw">msw</a> (who also made a snide remark) when I asked about efficiency, which I considered on the border of abuse. My code may or may not be reused by others, but I'm trying to make it easy to read and maintainable. I also have a bizarre desire to make my code as efficient as possible where possible. Hence the questions on efficiency and eloquence.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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