• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Using apsting.h in c++ (devcpp) to go from binary to decimal.

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Semyaza

Member
Joined
Mar 7, 2004
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.
 
You arn't initializing the pos values to anything. So when a value isn't 1, posn is a random value. One thing you can do is add an else block to each if statement.
Code:
if (binary[0] == 1)
{
  pos1 = 128;
} else {
  pos1 = 0;
}

You should also be checking binary[n] against the character '1' and not the integer value 1.
Code:
if (binary[0] == '1')
 
Just a question: what is apstring? C++ has a perfectly fine string library so why another one, especially for learning purposes?
 
yeah

I figured both of those out last night but thanks for responses. Btw I have a new problem for some reason when I enter 2 at my menu my prog either drops or goes doesnt pass my check for case2. Any ideas on that one?

#include <iostream.h>
#include "apstring.h"

int main()
{
char stop;
int menu;

cout << "Please type in either option 1 or 2.\n";
cout << "Option 1: Decimal to binary conversion.\n";
cout << "Option 2: Binary to decimal converstion.\n";
cin >> menu;
system ("cls");

switch(menu)
{
case 1:
//set up variables
int number, number2, number3, number4, number5, number6, number7, number8;
int check, check2, check3, check4, check5, check6, check7, check8;

//get number
cout << "Please enter number between 0 and 255.\n";
cin >> number;

//check if number is 0-255
if (number < 0 || number > 255)
{
system ("cls");
cout << "Number not valid, please exit and restart.";
cin >> stop;
}

//calculations
check = number / 128;
number2 = number % 128;

check2 = number2 / 64;
number3 = number2 % 64;

check3 = number3 / 32;
number4 = number3 % 32;

check4 = number4 / 16;
number5 = number4 % 16;

check5 = number5 / 8;
number6 = number5 % 8;

check6 = number6 / 4;
number7 = number6 % 4;

check7 = number7 / 2;
number8 = number7 % 2;

check8 = number8;

//Display results
cout << "Your number in binary is,\n" << check << check2 << check3 << check4 << check5 << check6 << check7 << check8;
cin >> stop;
break;
case 2:
//setup variables
int pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, total;
apstring binary;
//give pos variables default values
pos1 = 0;
pos2 = 0;
pos3 = 0;
pos4 = 0;
pos5 = 0;
pos6 = 0;
pos7 = 0;
pos8 = 0;

//ask for number
cout << "Please input 8 digit binary number(ex: 10010110) : \n";
getline(cin, binary);

//Check to see if number is only ones and 8 long.
if (binary.length() != 8)
{
system ("cls");
cout << "Invalid number, please restart.";
cin >> stop;
}

if (binary(0) != '0' || binary(0) != '1')
{
system ("cls");
cout << "Invalid number, please restart.";
cin >> stop;
}

if (binary(1) != '0' || binary(1) != '1')
{
system ("cls");
cout << "Invalid number, please restart.";
cin >> stop;
}

if (binary(2) != '0' || binary(2) != '1')
{
system ("cls");
cout << "Invalid number, please restart.";
cin >> stop;
}

if (binary(3) != '0' || binary(3) != '1')
{
system ("cls");
cout << "Invalid number, please restart.";
cin >> stop;
}

if (binary(4) != '0' || binary(4) != '1')
{
system ("cls");
cout << "Invalid number, please restart.";
cin >> stop;
}

if (binary(5) != '0' || binary(5) != '1')
{
system ("cls");
cout << "Invalid number, please restart.";
cin >> stop;
}

if (binary(6) != '0' || binary(6) != '1')
{
system ("cls");
cout << "Invalid number, please restart.";
cin >> stop;
}

if (binary(7) != '0' || binary(7) != '1')
{
system ("cls");
cout << "Invalid number, please restart.";
cin >> stop;
}

if (binary(8) != '0' || binary(8) != '1')
{
system ("cls");
cout << "Invalid number, please restart.";
cin >> stop;
}


//use if statments to find ones
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;
}

//calculate total
total = pos1 + pos2 + pos3 + pos4 + pos5 + pos6 + pos7 + pos8;
//display total
cout << "Binary number equals " << total << ".\n";
//pause and end program
cin >> stop;
break;
}
cin >> stop;
return 0;
}
 
When I try to run it, I get the "Invalid number, please restart." error. This is because you are using the || operator in that if statement. Think about the logic there. You are also checking indexes 0 through 8. This totals 9 if statements, but you only have 8 values.

The problems are all very easy to track down once you get used to using the debugger. Its a little complicated to learn, but the rewards are great.
 
mccoyn said:
When I try to run it, I get the "Invalid number, please restart." error. This is because you are using the || operator in that if statement. Think about the logic there. You are also checking indexes 0 through 8. This totals 9 if statements, but you only have 8 values.

The problems are all very easy to track down once you get used to using the debugger. Its a little complicated to learn, but the rewards are great.

lol doh i was thinking 8 so I went to 8. Didnt solve it but what do you mean my logic?

if (binary(0) != '0' || binary(0) != '1')
{
system ("cls");
cout << "Invalid number, please restart.";
cin >> stop;
}

Doesn't that mean if the first char in binary is not 0 or 1 to print out that invalid message?

I think something else is wrong too, cause when I completely removed my check step, it just stopped running completely when it should have started doing the binary conversion.

Ty for help so far.
 
Semyaza said:
if (binary(0) != '0' || binary(0) != '1')
{
system ("cls");
cout << "Invalid number, please restart.";
cin >> stop;
}

Doesn't that mean if the first char in binary is not 0 or 1 to print out that invalid message?

What it means is if the first char in binary is not 0 or the first char in binary is not 1 ... The important thing is that they are individual checks, not factored together like how you wrote it. The first part will be true for anything but '0'. The second part will be true for '0'. When you or two numbers together if one of them is true the result will be true. Since every possible values for binary(0) will result in at least one of those being true, then the result will always be true.
 
mccoyn said:
What it means is if the first char in binary is not 0 or the first char in binary is not 1 ... The important thing is that they are individual checks, not factored together like how you wrote it. The first part will be true for anything but '0'. The second part will be true for '0'. When you or two numbers together if one of them is true the result will be true. Since every possible values for binary(0) will result in at least one of those being true, then the result will always be true.

DOH good point, lol your pretty good at this stuff. This class is my first experiance with programming, besides like playing with python and some linux script. :p

Hrm.... how can I phrase it then.

Can I make multiple characters for it to be?
Something like
(binary[0] == '2' || '3' || '4' || '5' || '6' || '7' || '8' || '9')
 
Last edited:
Semyaza said:
Can I make multiple characters for it to be?
Something like
(binary[0] == '2' || '3' || '4' || '5' || '6' || '7' || '8' || '9')
That won't work. You can use the and (&&) operator in your if statements. So it will read like if binary[0] is not '0' and binary[0] is not '1'. I remember working through the same situation a few years ago.
 
mccoyn said:
That won't work. You can use the and (&&) operator in your if statements. So it will read like if binary[0] is not '0' and binary[0] is not '1'. I remember working through the same situation a few years ago.

Thanks for all your help. Got a lot of wrinkles out its still messing up for some reason though. This is getting annoying lol.
 
Found out what was wrong besides all that other junk its working fine now. I had to clear the keyboard buffer.
cin.ignore(80,'\n');
 
Back