Diffrence between cin and assignment
<p>Consider the following 2 code snippets:</p>

<p>Case 1:</p>

<pre><code>#include &lt;iostream&gt;
int main()
{
    int i=0;
    char c='a';
    i=c;
    cout &lt;&lt; i &lt;&lt; endl; //Retuens ASCII value of 'a'
    return 0;
}
</code></pre>

<p>Case 2:</p>

<pre><code>#include &lt;iostream&gt;
int main()
{
    cout &lt;&lt; "Enter integer value" &lt;&lt; endl;
    int i=-1;
    cin &gt;&gt; i; //Assume user enters 'a'
    cout &lt;&lt; i &lt;&lt; endl; //prints -1 on screen
    return 0;
}
</code></pre>

<p>In Case 1, when we use assignment the ASCII equivalent of <code>'a'</code> is assigned to <code>int i</code>, but in Case 2 <code>int i</code> is <code>-1</code>. Why is the behavior different in both the cases? Is it by design? Is it possible (with standard functions) to input ASCII value using <code>cin</code> when characters are input for integer variables?</p>

<p>// I understand that cin is failing. What I would like to know is: why cin fails when char is entered, if assignment properly assigns ascii value?</p>
