#include <iostream>
using namespace std;
int main()
{
  cout << "enter" << endl;
  int a,b;
  cin >> a >> b;
  cout << a << " " << b << endl;
  return 0;
}

in the code given above when the input is
INPUT:
45 12
OUTPUT:
45 12

but now let say input is
INPUT:
45 s12
OUTPUT:
45 134514832
from where i am getting this long number 134514832

#include <iostream>
using namespace std;
int main()
{
  cout << "enter" << endl;
  int a,b;
  cin >> a >> b;
  cout << a << " " << b << endl;
  return 0;
}

in the code given above when the input is
INPUT:
45 12
OUTPUT:
45 12

but now let say input is
INPUT:
45 s12
OUTPUT:
45 134514832
from where i am getting this long number 134514832

1)on my computer, the output of the second number always comes as 13, even if I enter something else.
2)the output you're getting is because you've declared the variables a & b as of int type, but when you enter "s" as the input, it is in char type.So the program converts the character into a number, as variables a & b are of int type and not char.

However, it should convert the character into its ASCII code, but I don't know why does it give such an odd output.

I think that the number that you get out when you do this is undefined and compiler specific. For example, I just get 0, which I think is the standard thing that I get whenever the conversion to int can't be done. The large number is just a kind of noise that you shouldn't really pay attention to.

If you're at all concerned that the input that you get from the user might not be sane, then the thing to do is probably to read the input as a string and then manually check that it makes sense. If you're expecting an int , then you might want to check that all the characters are numbers, for example:

#include <iostream>
#include <string>
#include <sstream>

bool isDigit( char ch ){    return ( ch >= '0' ) && ( ch < '0' + 10 );  }

int main()
{  
  std::string a_str, b_str;
  
  std::cout << "enter" << std::endl;
  std::cin >> a_str >> b_str;

  /* Check the numbers for sanity */
  for( std::string::size_type i = 0; i < a_str.size(); ++i ){
      if ( isDigit( a_str[i] ) == false ){
          std::cerr << "Error: \"" << a_str << "\" is not a valid number!" << std::endl;
          return 1;
      }
  }

  for( std::string::size_type i = 0; i < b_str.size(); ++i ){
      if ( isDigit( b_str[i] ) == false ){
          std::cerr << "Error: \"" << b_str << "\" is not a valid number!" << std::endl;
          return 1;
      }
  }

  /* Safe to convert the strings to numbers */
  
  std::stringstream a_ss( a_str );
  std::stringstream b_ss( b_str );
  int a_int, b_int;

  /* Do the conversion */
  a_ss >> a_int;
  b_ss >> b_int;

  /* Do some arithmetic to prove that they're numbers now :o) */
  std::cout << a_str << "+" << b_str << " = " << a_int + b_int << std::endl;

  return 0;
}

Hope that makes sense to you :o)

So the program converts the character into a number, as variables a & b are of int type and not char.

However, it should convert the character into its ASCII code, but I don't know why does it give such an odd output.

It does no such thing. cin produces an error and does nothing at all, leaving the value of the integer unchanged. The value that was shown in the output was just some random value that was in that memory location when main() was entered from the program's startup code -- which is the code that the compiler adds to the program and is executed before main().

You can easily verify this yourself by initializing the variables when they are declared, such as int a = 1, b = 2;

commented: Agreed, using an "int" is not for storingletters and symbols, it's is used for storing non decimal numbers. When given another data type, it can't hold it, so it holds the value of the memory location where the data is stored +2
commented: Thanx, didn't know about it. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.