Help with an assignment

cout << "Now give me a base 2 integer between 0 and 11111: ";
int x;
cin >> x;
int l = 0;
// Will take the binary and turn it into a decimal.
// I used "dfifth" for the same reason I used "fifth" earlier, but this is a decimal, so I put d first.
int dfifth = x / 10000;
l = l + (16 * dfifth);
x = x + (dfifth / 10000);

int dfourth = x / 1000;
l = l + (8 * dfourth);
x = x + (dfourth / 1000);

int dthird = x / 100;
l = l + (4 * dthird);
x = x + (dthird / 100);

int dsecond = x / 10;
l = l + (2 * dsecond);
x = x + (dsecond / 10);

int dfirst = x / 1;
l = l + (1 * dfirst);
x = x + (dfirst / 1);

cout << "In decimals, that is: ";
cout << l;
cout << "\n";

system("PAUSE");
return 0;

This is after another part of the program that worked. I have #include<iostream>; and using namspace std; and all the necessary setup.
It's supposed to convert binary into a base 10 integer, but it doesn't. What is wrong with it?
These kind of conversions are much easier when you take the input as an std::string. I don't suggest you copy this code verbatim. You tried, and although you made progres, you wandered a bit too far off in the wrong direction. So here is a reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <algorithm>


double BinToDec(std::string Arg)
{
    double Value = 0;
    
    std::string Input = Arg;
    std::reverse(Input.begin(), Input.end());
 
    for(unsigned i = 0; i < Input.length(); ++i)
    {
        if(Arg[i] != '1' && Arg[i] != '0')
        {
            throw i;
        }
        
        if(Input[i] == '1')
        {
            Value = Value + pow(2, (i));
        }
    }
    
    return Value;
}


int main()
{
    std::string Input;
    
    std::cout << "Input Binary Number\n";
    std::cin >> Input;
    
    try
    {
        std::cout << BinToDec(Input) << '\n';
    }
    catch(unsigned I)
    {
        std::cout << "Error found. Non-binary input at position " << (I+1) << "\n";
    }
    
    return 0;
}


Do you see how a little awareness of the std library makes these seemingly impossible tasks trivial?
Last edited on
Except this is an intro class and he wants us to only use what we've already learned (we're not supposed to use strings). This is how the first part was done and it works. I'm just trying to alter it so that it turns binary into the other numbers.
#include<iostream>;

using namespace std;

int main()
{
cout << "Enter a base 10 integer between 0 and 31: ";

int n;
cin >> n;
int b;
b = n;

// Will convert the number n into binary.
int m = 0;
// Will create the first (from left to right) 1 or 0 (10,000's place) of binary.
// I chose "fifth" because it is fifth from right to left.
int fifth = n / 16;
m = m + (fifth * 10000);
n = n - (16 * fifth);
// Will create the second 1 or 0 (1,000's place) of binary.
int fourth = n / 8;
m = m + (fourth * 1000);
n = n - (8 * fourth);
// Will create the third 1 or 0 (100's place) of binary.
int third = n / 4;
m = m + (third * 100);
n = n - (4 * third);
// Will create the fourth 1 or 0 (ten's place) of binary.
int second = n / 2;
m = m + (second * 10);
n = n - (2 * second);
// Will create the fifth 1 or 0 (one's place) of binary.
int first = n / 1;
m = m + (first * 1);
n = n - (1 * first);

cout << "You've entered a value of ";
cout << b;
cout << " in base 10. In base 2, that number is ";
cout << m;
when i had assignments like this i always asked my math professor and he always helped me out with some crazy formula or would help me figure out the possibilities.
Sometimes it helps to trace a specific example. Suppose the user enters 10011 for x. Then here are the values of the variables after the first few lines:

1
2
3
4
5
6
7
8
9
10
int l = 0;
// Will take the binary and turn it into a decimal.
// I used "dfifth" for the same reason I used "fifth" earlier, but this is a decimal, so I put d first.
int dfifth = x / 10000;   // dfifth==1
l = l + (16 * dfifth);    // l == 16
x = x + (dfifth / 10000); // x == 10011

int dfourth = x / 1000;   // dfourth == 10
l = l + (8 * dfourth);    // l == 96
x = x + (dfourth / 1000); // x == 10011 
I actually figured it out. I needed a - instead of + with the x statements and a * instead of a /.
Thanks for trying to help, guys :)
Topic archived. No new replies allowed.