In my function to check if the input value was binary or not, I am struggling with the how i should do that exactly. What should I put as my else if condition?
or would it be easier to implement the check in the actual conversion function? I am 'supposed' to implement the check in a seperate function, but I am not sure on how to do it unless I actually calculate it
For GetBin(), I think you have to resort to using char and/or string:
1. Offline algorithm: You can have the user input digits into a string variable. Then loop through the string and test to see if any of the char are not '1' or '0'.
2. Online algorithm: You can modify the do-while loop so that you cin >> ch, where ch is a char type instead of a long long. For each user cin, you check to see if it is '1' or '0'. And if it is '1' or '0', add it to a string. The user will keep on inputting '1' or '0' until they enter an invalid data. You can test if the data is invalid and exit the loop if it is, or do whatever else you want.
elseif (n%10 != 0 || n%10 != 1)
The conditional above won't catch all non-binary numbers, e.g., 150 % 10 = 0. So even though 150 is not binary the condition above won't catch this fact.
Yea, i realized my else if function right now doesn't work for most numbers, thank you. Could I convert my long long n in my else if function to a string inside the loop, and check each char there?
string GetBin()
{
string user_input;
bool baddata = false;
//ask the user here.
//no need to loop cin for offline algorithm
//user enters all data at once
cin >> user_input;
for(int i = 0; i < user_input.size(); i++)
{
if(user_input[i] != "0" || user_input[i] != "1" )
baddata = true
//check to see if baddata is true
//if it is, reset counter i
//alternatively use a while loop
}
return user_input; //return binary data as string
}
Note: You probably want to keep binary number as string in case you need to evaluate each bit in the future.
I decided to create a new function for this. It is saying I need pointer to for my 'i' and my .size isn't working, do I need a new header? Also, where would I put the function call in my program, right before or after when i call GetBin()?
Create a binary parser function to convert string of bits to decimal:
1 2 3 4 5 6 7 8 9 10 11 12
longlong stringBitstoDecimal(string n)
{
longlong decimal;
//go through each place in string using loop
//you can do this because string is an array of characters, so you can use [] operator to access each element.
//accumulate decimal value in decimal
return decimal;
}
If you need to learn what string is and how to use it go here:
I would put it in the same function as GetBin(), but I wouldnt know how to check if it was negative? Would I still have to convert it to check or is there a way to check if a string is '0' ?
Ok, thank you a lot. I will try implementing that now. Also, I tried thinking of it a different way similar to my first way (which was wrong). Why doesn't this work now?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void test_bin(longlong n)
{
longlong ans;
while (n != 0 && n != 1)
{
ans = n % 10;
if (ans > 1)
{
n %= 10;
}
else
cout << "That is not a binary number" << endl;
}
}
That works, but you're trading run time for memory. And you lose the ability to edit the bits efficiently. Each time you want to recalculate the bits, you need to perform modulus operations. Whereas in a string, you're simply accessing the bits. Notice that you need to go through each bit to convert to decimal.
But it all comes down to what you want for your program. If you don't need to edit bits or do a lot of bit calculations, then use the test_bin() you have created.
The reason why string is better is because you can implement more simple bitwise operations. E.g., 001010 AND 100000 = 101010. With string implementation, you just compare each bit. In modulus implementation, you have to do modulus to both simultaneously before you can compare. But string uses more memory. This is a trade off you have to decide on.
However, in this day and age, memory is usually more expendable than time.
EDIT: I forgot to say this, if you're not using negative integers, use unsigned long long.
I don't think that that matters much for this project, but I will think about that in my future endeavors. However, when i type in a non-binary number (ex. 555), my program does not give me an error message. It goes through with the calculation (I think). Also, sorry to always post my whole program, I am not sure which parts are pertinent to my question usually.
#include "stdafx.h"
#include<iostream>
#include<limits>
usingnamespace std;
usingnamespace System;
void ConvertByRecursion(longlong n);
longlong getDecimal();
longlong GetBin();
void bin_dec(longlong n);
void test_bin(longlong n);
int main()
{
longlong n;
int ans;
string k;
cout << "Welcome to my decimal to binary converter" << endl;
cout << "Please hit 1 for decimal to binary, 2 for binary to decimal, and 0 to quit" << endl;
cin >>ans;
if (ans == 1)
{
n =
n = getDecimal();
while (n != 0)
{
ConvertByRecursion(n);
cout << endl << endl;
n = getDecimal();
}
}
elseif (ans == 2)
{
test_bin(n);
n = GetBin();
while (n !=0)
{
bin_dec(n);
n = GetBin();
}
}
else
cout << "Thanks for using my converting program" << endl;
system("Pause");
return 0;
}
longlong getDecimal()
{
longlong n;
bool baddata = false;
do
{
baddata = false;
cout << "Please enter your decimal value to convert: ";
cin >> n;
if (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "I could not decifer your input as a decimal value" << endl;
baddata = true;
}
elseif (n < 0)
{
cout << "Please enter a non-negative number" << endl;
baddata = true;
}
} while (baddata);
return n;
}
void ConvertByRecursion(longlong n)
{
longlong r;
longlong newval;
r = n % 2;
newval = n / 2;
Console::WriteLine("Decimal {0,12:D} divided by 2 ={1,12:D} w/ remainded of {2,3:D} ", n, newval, r);
if (newval>0)
{
ConvertByRecursion(newval);
}
else
{
cout << "The binary value is: ";
}
cout << r;
}
longlong GetBin()
{
longlong n;
bool baddata = false;
do
{
baddata = false;
cout << "Please input the binary number to be converted to decimal: ";
cin >> n;
if (n < 0)
{
cout << "Please input a non-negative number" << endl;
baddata = true;
}
elseif (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "I could not decifer your input as a binary value" << endl;
baddata = true;
}
} while (baddata);
return n;
}
void bin_dec(longlong n)
{
double num = 0;
double rem;
int i = 0;
while (n != 0)
{
rem = n % 10;
n /= 10;
num += (rem)*pow(2, i);
++i;
}
cout << "The decimal value is: " << num << endl;
}
void test_bin(longlong n)
{
longlong ans;
while (n != 0 && n != 1)
{
ans = n % 10;
if (ans > 1)
{
n %= 10;
}
else
cout << "That is not a binary number" << endl;
}
}