|  |
12-13-2006, 09:40 PM
|
#1 (permalink)
|
Newb Techie Join Date: Aug 2006 Posts: 9
| 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? |
| |
12-13-2006, 09:43 PM
|
#2 (permalink)
|
Ultra Techie Join Date: May 2006 Posts: 924
|
__________________ |
| |
12-15-2006, 10:19 AM
|
#3 (permalink)
|
Newb Techie Join Date: Aug 2006 Posts: 9
| 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;
} |
| |
12-15-2006, 10:30 AM
|
#4 (permalink)
|
Master Techie Join Date: May 2005 Location: UK Posts: 2,749
| 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]
__________________ |
| |
12-15-2006, 11:12 AM
|
#5 (permalink)
|
Newb Techie Join Date: Aug 2006 Posts: 9
| 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. |
| |
12-15-2006, 11:16 AM
|
#6 (permalink)
|
Master Techie Join Date: May 2005 Location: UK Posts: 2,749
| 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?
__________________ |
| |
12-15-2006, 11:20 AM
|
#7 (permalink)
|
Master Techie Join Date: May 2005 Location: UK Posts: 2,749
| 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:
__________________ |
| |
12-15-2006, 08:12 PM
|
#8 (permalink)
|
Software Developer Join Date: Mar 2006 Location: Columbus, OH Posts: 569
| 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: 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: |
| |
12-17-2006, 06:03 PM
|
#9 (permalink)
|
Newb Techie Join Date: Aug 2006 Posts: 9
| 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. |
| |
12-18-2006, 01:22 AM
|
#10 (permalink)
|
Monster Techie Join Date: May 2004 Location: /usr/root/mn/us Posts: 1,121
| 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!!!! |
| |  | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |