C++/c++

Advertisement


Question
when i m running this code it is reversing bits properly for 5 or less than 5 number of bits. it is not working properly for more than 5 no. of bits.why it is not working properly and how can i overcome this problem?

#include<iostream.h>
#include<conio.h>
class dencoding
{
int a,b,rem,rev;
public:
dencoding()
{
a=0;
b=0;
rem=0;
rev=0;
}
void getdata()
{
cout<<"\n enter ur bit code";
cin>>a;
while(a>0)
{
rem=a%10;
a/=10;

rev=rev*10+rem;
}
cout<<rev;
getch();
}
};
int main()
{
clrscr();
dencoding d;
d.getdata();
return 0;
}

Answer
Probably because the maximum value of an int in the C++ implementation that you are using is limited to 5 digits. This would be the case, for example, if the int is a 16-bit integer - INT_MAX would be 32,767.

Check this out by printing out INT_MAX ( header <limits.h> ). see: http://en.wikipedia.org/wiki/Limits.h

If this is the case, you may be able to use a long instead an int to allow more digits - in this case, LONG_MAX would normally be 2,147,483,647 allowing up to ten digits in a long.

You seem to be using an obsolete C++ compiler - it would certainly be non-conforming and you should certainly switch to a more modern compiler. I presume you are using a windows machine, in this case you could download a free good compiler + IDE from hre: http://prdownload.berlios.de/codeblocks/codeblocks-8.02mingw-setup.exe  

note: <iostream.h> is a pre-standard C++ header and is not part of standard C++ - use the header <iostream> instead. <conio.h> is not, and has never been a standard C or C++ header.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.