input problem

A

Al

I am typing the following code to convert a binary number to decimal.
It usually works fine, but with some numbers, it reads strange values:

#include <iostream>
using namespace std;

int main()
{
int binar;
cin >> binar;
// cout <<binar <<endl;
int dec=0, num=1;
dec += (binar%10) * 1;
binar /= 10;
while (binar>1)
{
dec += (binar%10) * num*2;
num*=2;
binar /= 10;
}
dec += binar * num*2;
cout << dec <<endl;
system("pause");
return 0;
}

when I debug or I use the comment line with a value: 10011001100, it
prints (the value of binar is) 2 and the decimal is also 2.


Same problem with the following code in C, but the values are a lot
different: 1421066508 for binar value and 2028 for dec:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int bin, dec=0, num=1;
scanf("%d", &bin);
printf("%d\n", bin);
dec += (bin%10) * 1;
bin /= 10;
while (bin>1)
{
dec += (bin%10) * num*2;
num*=2;
bin /= 10;
}
dec += bin * num*2;
printf("%d\n", dec);
system("pause");
return 0;
}



do you know what the problem is??
is it for overflow of integer?
help!
 
J

Jim Langston

Al said:
I am typing the following code to convert a binary number to decimal.
It usually works fine, but with some numbers, it reads strange values:

#include <iostream>
using namespace std;

int main()
{
int binar;
cin >> binar;
// cout <<binar <<endl;
int dec=0, num=1;
dec += (binar%10) * 1;
binar /= 10;
while (binar>1)
{
dec += (binar%10) * num*2;
num*=2;
binar /= 10;
}
dec += binar * num*2;
cout << dec <<endl;
system("pause");
return 0;
}

when I debug or I use the comment line with a value: 10011001100, it
prints (the value of binar is) 2 and the decimal is also 2.

Max the max range on an int? For signed it's 2147483647 on Microsoft
systems anyway. 10011001100 is greater than 2147483647. You are
overflowing your int.

I would suggest you use std::string or char array instead.
 
A

Al

Jim said:
I would suggest you use std::string or char array instead.

how could I use strings for manipulating numbers??
Should I consider them numbers?or do I need to access to each element
of the array?
could you show me a little example?
 
B

Brian - qbg

One thing you could do is use a bigger datatype, but that would still
have it limits.
 
K

Karl Heinz Buchegger

Al said:
how could I use strings for manipulating numbers??

Jim suggests to use the string only for the binary 'number'.

std::string binar;
cin >> binar;

If you need to get the value of an digit, you can simply
calculate it.
Say your string contains "1001"
and you need the numerical value of the digit at index position
2, you can simply do
int digit = binar[2] - '0';

now digit will only be 0 or 1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top