Alright so I have this project I am supposed to use a switch to make a menu to let a user either convert decimal to binary or vice versa. I have everything done but the binary to decimal conversion.
We are supposed to get a binary number of max 8 chars and convert it to decimal using apstring. Can't use arrays at all just apstring, or loops, we do not 'learn' those till next week.
So heres what I got so far any ideas?
#include <iostream.h>
#include "apstring.h"
int main()
{
char stop;
int pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, total;
apstring binary;
cout << "Please input 8 digit binary number(ex: 10010110) : \n";
getline(cin, binary);
if (binary[0] == 1)
{
pos1 = 128;
}
if (binary[1] == 1)
{
pos2 = 64;
}
if (binary[2] == 1)
{
pos3 = 32;
}
if (binary[3] == 1)
{
pos4 = 16;
}
if (binary[4] == 1)
{
pos5 = 8;
}
if (binary[5] == 1)
{
pos6 = 4;
}
if (binary[6] == 1)
{
pos7 = 2;
}
if (binary[7] == 1)
{
pos8 = 1;
}
total = pos1 + pos2 + pos3 + pos4 + pos5 + pos6 + pos7 + pos8;
cout << "Binary number equals " << total << ".\n";
cin >> stop;
return 0;
}
I just keep getting some random number that means nothing.
We are supposed to get a binary number of max 8 chars and convert it to decimal using apstring. Can't use arrays at all just apstring, or loops, we do not 'learn' those till next week.
So heres what I got so far any ideas?
#include <iostream.h>
#include "apstring.h"
int main()
{
char stop;
int pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, total;
apstring binary;
cout << "Please input 8 digit binary number(ex: 10010110) : \n";
getline(cin, binary);
if (binary[0] == 1)
{
pos1 = 128;
}
if (binary[1] == 1)
{
pos2 = 64;
}
if (binary[2] == 1)
{
pos3 = 32;
}
if (binary[3] == 1)
{
pos4 = 16;
}
if (binary[4] == 1)
{
pos5 = 8;
}
if (binary[5] == 1)
{
pos6 = 4;
}
if (binary[6] == 1)
{
pos7 = 2;
}
if (binary[7] == 1)
{
pos8 = 1;
}
total = pos1 + pos2 + pos3 + pos4 + pos5 + pos6 + pos7 + pos8;
cout << "Binary number equals " << total << ".\n";
cin >> stop;
return 0;
}
I just keep getting some random number that means nothing.