Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 12-13-2006, 09:40 PM   #1 (permalink)
 
Newb Techie

Join Date: Aug 2006

Posts: 9

el_presidente

Default binary 2 decimal

I'm trying to write a program in MS visual c++ that will convert a binary string into a decimal. I know what the formula is and all that, I just can't think of anything that can access each member of the string and applying the formula. I've tried using arrays, but I can't seem to get this to work. What's the simplist way to do this?
el_presidente is offline  
Old 12-13-2006, 09:43 PM   #2 (permalink)
kfc469's Avatar
 
Ultra Techie

Join Date: May 2006

Posts: 924

kfc469

Send a message via AIM to kfc469
Default

google is a wonderful tool. first thing: http://www.permadi.com/tutorial/numBinToDec/index.html
__________________

kfc469 is offline  
Old 12-15-2006, 10:19 AM   #3 (permalink)
 
Newb Techie

Join Date: Aug 2006

Posts: 9

el_presidente

Default

Thanks for the help. I got it to work, but I had to make it so you have to enter each member of the array at a time. It calulates only for a four digit binary number correctly, but theres a run-time error every time. How can I make it so I can enter how ever many digits I want, and get rid of the error. Here's what I got.

#include <iostream>
using namespace std;
int main()
{
int hold1;
int hold2;
int hold3;
int hold4;
int main_hold;
int BinString[3];
cin >> BinString[0];
cin >> BinString[1];
cin >> BinString[2];
cin >> BinString[3];
hold1 = BinString[3] * 1;
hold2 = BinString[2] * 2;
hold3 = BinString[1] * 4;
hold4 = BinString[0] * 8;
main_hold = hold1 + hold2 + hold3 + hold4;
cout << main_hold << endl;
return 0;
}
el_presidente is offline  
Old 12-15-2006, 10:30 AM   #4 (permalink)
baronvongogo's Avatar
 
Master Techie

Join Date: May 2005

Location: UK

Posts: 2,749

baronvongogo is on a distinguished road

Default

binstring should be 4 shouldnt it?

your holding 4 values in an array that can only hold 3, it will still be 0,1,2,3 just the initial variable int binstring should be [4]
__________________
baronvongogo is offline  
Old 12-15-2006, 11:12 AM   #5 (permalink)
 
Newb Techie

Join Date: Aug 2006

Posts: 9

el_presidente

Default

I don't think so. It counts BinString[0] as a member of the array, because computers count 0,1,2,3,4,5 and so on, while most people forget about the zero. That's why if you were to declare the array as int BinString[0]; there is only one member in it.
el_presidente is offline  
Old 12-15-2006, 11:16 AM   #6 (permalink)
baronvongogo's Avatar
 
Master Techie

Join Date: May 2005

Location: UK

Posts: 2,749

baronvongogo is on a distinguished road

Default

i thought it was something like this:

int age[5] ={1,2,3,4,5};

then say you want to access one it would be

age[0]?

or is that just another way of doing it?
__________________
baronvongogo is offline  
Old 12-15-2006, 11:20 AM   #7 (permalink)
baronvongogo's Avatar
 
Master Techie

Join Date: May 2005

Location: UK

Posts: 2,749

baronvongogo is on a distinguished road

Default

ok I did it your way and got 26 then run time error changed it to 4 got 26 but no run time error :beard:
__________________
baronvongogo is offline  
Old 12-15-2006, 08:12 PM   #8 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

Quote:
That's why if you were to declare the array as int BinString[0]; there is only one member in it.
Actually, it means you've declared an array that can hold no values.

Quote:
binstring should be 4 shouldnt it?
Yes.

el_presidente, you're confusing the difference between accessing and declaring an array. First, when you declare an array, the number in the brackets specifies the number of elements the array can hold. For instance:
Code:
int array[5];
declares an integer array that can hold 5 integer elements.

On the other hand, when you want to access the 5th element in the array, you would use the following syntax:
Code:
array[4];

jaeusm is offline  
Old 12-17-2006, 06:03 PM   #9 (permalink)
 
Newb Techie

Join Date: Aug 2006

Posts: 9

el_presidente

Default

yea, i think i did mix that up. is there any way i can enter the entire thing all in one shot rather than pressing enter after each time and so i can enter as many as i need to.
el_presidente is offline  
Old 12-18-2006, 01:22 AM   #10 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default

Yup, you can just read the enitre string in at the beginning. I'm not familiar with C++, but in C you would just do a scanf to read in the string.

Then you can access each character of the string as an element of an array.

So something like the following.
This is in C
Code:
int i;
double sum;
char str[100];

for(i=0, i<100; i++)
   char[i]=0;

scanf("%s", str);

for(i = 0, i < 100, i++){
   sum += char[i] * pow(2, i);
}

return sum

double pow(int base, int exp){
   double sum = 1;
   int i;
   for(i=0, i<exp, i++)
      sum = sum*base;

  return sum;
}
My syntax might be a bit off since I haven't coded in a bit, but the format is correct.
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On