Results 1 to 11 of 11

101204, 09:44 PM #1
 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.

101304, 07:18 AM #2
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; }
Code:if (binary[0] == '1')

101304, 01:02 PM #3
 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?

101304, 05:14 PM #4
 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 0255
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;
}

101304, 06:25 PM #5
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.

101404, 01:08 AM #6
 Join Date
 Mar 2004
Originally Posted by mccoyn
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.

101404, 07:15 AM #7Originally Posted by Semyaza

101404, 05:43 PM #8
 Join Date
 Mar 2004
Originally Posted by mccoyn
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; 101404 at 06:14 PM.

101404, 08:10 PM #9Originally Posted by Semyaza

101504, 01:11 AM #10
 Join Date
 Mar 2004
Originally Posted by mccoyn

101804, 07:05 PM #11
 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');