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:
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