Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:28:16+00:00 2026-05-23T19:28:16+00:00

Consider the following 2 code snippets: Case 1: #include <iostream> int main() { int

  • 0

Consider the following 2 code snippets:

Case 1:

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

Case 2:

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

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

// 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:28:17+00:00Added an answer on May 23, 2026 at 7:28 pm

    When the user enters 'a', the operator implementation tries to convert this to an integer, and fails.

    In response to the comment (which seems to be the main point of your question):

    yes, 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?

    'a' is not “an ASCII value”. In the C language, it is a character literal.

    But cin doesn’t know what a character literal is. As far as cin can tell, the user entered this:

    const char* inputValue = "'a'";
    

    cin is not a C++ compiler. It is much simpler, and you might argue much stupider. When you tell it to stream in an integer, it is going to stream in an integer (and only an integer). If that integer happens to correspond to a character in the ASCII table, so be it, but the user must enter it as 85.

    The fact that the stream input operators are “stupid” is actually a good thing, because:

    • They execute faster
    • It is simpler for an experienced programmer to understand what goes on under the hood
    • You can be darn sure the user entered a number (fewer bugs)

    The stream input operators (>>) aren’t just used for console input. They are also used for file input, which needs to be fast, and secure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: #include <stdio.h> int main (void) { char str1[128], str2[128], str3[128];
Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider the following code: template <int dim> struct vec { vec normalize(); }; template
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
Consider the following Cocoa/Obj-C code snippets: MyClass *obj; @try { [obj doSomething]; } @catch
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Consider the following snippets of code: Exhibit A: $_REQUEST = json_decode(stripslashes(json_encode($_REQUEST, JSON_HEX_APOS)), true); Exhibit
Consider the following code: class Program { static void Main(string[] args) { A a
Consider following code: char str[] = Hello\0; What is the length of str array,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.