Computing.Net > Forums > Programming > C++ Question

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ Question

Reply to Message Icon

Name: Belal
Date: December 3, 2003 at 07:50:32 Pacific
OS: Mac OS X
CPU/Ram: 800 700
Comment:

Hello;
I have this simple code:

int a;
cin >> a;
cout << a;

if I enter a char (r) instead of an int (23) I get a
whole number : 3154592
I know that i'm entering a wrong variable type, but
can any one explain what's going behind the
scene??
thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: retrogamer
Date: December 3, 2003 at 13:04:53 Pacific
Reply:

I know that Perl automatically converts non-numerical characters to zero when working with numbers...maybe this is doing similar?


0

Response Number 2
Name: Ronin1
Date: December 3, 2003 at 13:07:10 Pacific
Reply:

something like

cin >> a;

if(cin.fail())
{
cin.clear(1);
cin.ignore(80);
}

Does some error checking. Once stream errors occur, you need to reset the fail bit before other operations can be done. The underlying cause is most likely the way the data types are represented at the machine level, but I'm not positive.

You can reduce stream errors by using char types for numerical input, and then convert them using strtol, strtod, etc.


0

Response Number 3
Name: Sord
Date: December 3, 2003 at 20:21:43 Pacific
Reply:

Or you could change a to a character array.

Not a great example but it works:
char a[5];
int num;
bool usenum = false;
cin >> a;
if (a[0] >= '0' && a[0] <= '9')
{
usenum = true;
for (int i = 0; i < strlen(a); i++)
{
num *= 10;
num += a[i] - '0';
}
}
if (usenum == false)
cout << a;
else
cout << num;


0

Response Number 4
Name: Eric
Date: December 4, 2003 at 10:18:03 Pacific
Reply:

This may or may not help, but extractors,
inserters, and other overloaded operators
from the iostream lib are probably not
returning the value of the correct type. It
may be as simple as no implicit
conversion to a char (in which case you
would have to explicitly
downcast it using a static cast
(depending on the compiler you are
using.)) It may also be a decimal
representation of the address of a
uninitialized char or void pointer to
returned to cout (depending upon which
overloaded function is called
iac with how your overloaded left/right
shift operators are defined.)

You may also want to look at the behavior
of the derived 'ostringstream' definition of
class 'ios' in the iostream lib for your
compiler.

Eric


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ Question

C++ question(s) www.computing.net/answers/programming/c-questions/8642.html

A few C++ questions www.computing.net/answers/programming/a-few-c-questions/13898.html

c++ question... www.computing.net/answers/programming/c-question/3127.html