Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

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

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 10-12-04, 09:44 PM Thread Starter   #1
Semyaza
Member



Join Date: Mar 2004

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


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.
Semyaza is offline   QUOTE Thanks
Old 10-13-04, 07:18 AM   #2
mccoyn
Senior Member

 
mccoyn's Avatar 

Join Date: Nov 2003
Location: Michigan, USA

 
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')
mccoyn is offline   QUOTE Thanks
Old 10-13-04, 01:02 PM   #3
klingens
Member



Join Date: Apr 2002
Location: Xanadu

 
Just a question: what is apstring? C++ has a perfectly fine string library so why another one, especially for learning purposes?
klingens is offline   QUOTE Thanks
Old 10-13-04, 05:14 PM Thread Starter   #4
Semyaza
Member



Join Date: Mar 2004

 
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;
}
Semyaza is offline   QUOTE Thanks
Old 10-13-04, 06:25 PM   #5
mccoyn
Senior Member

 
mccoyn's Avatar 

Join Date: Nov 2003
Location: Michigan, USA

 
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 is offline   QUOTE Thanks
Old 10-14-04, 01:08 AM Thread Starter   #6
Semyaza
Member



Join Date: Mar 2004

 
Quote:
Originally Posted by mccoyn
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 is offline   QUOTE Thanks
Old 10-14-04, 07:15 AM   #7
mccoyn
Senior Member

 
mccoyn's Avatar 

Join Date: Nov 2003
Location: Michigan, USA

 
Quote:
Originally Posted by Semyaza
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 is offline   QUOTE Thanks
Old 10-14-04, 05:43 PM Thread Starter   #8
Semyaza
Member



Join Date: Mar 2004

 
Quote:
Originally Posted by mccoyn
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 by Semyaza; 10-14-04 at 06:14 PM.
Semyaza is offline   QUOTE Thanks
Old 10-14-04, 08:10 PM   #9
mccoyn
Senior Member

 
mccoyn's Avatar 

Join Date: Nov 2003
Location: Michigan, USA

 
Quote:
Originally Posted by Semyaza
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 is offline   QUOTE Thanks
Old 10-15-04, 01:11 AM Thread Starter   #10
Semyaza
Member



Join Date: Mar 2004

 
Quote:
Originally Posted by mccoyn
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.
Semyaza is offline   QUOTE Thanks
Old 10-18-04, 07:05 PM Thread Starter   #11
Semyaza
Member



Join Date: Mar 2004

 
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');
Semyaza is offline   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 12:51 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?