# Binary_Gray Conversion

1. Apr 21, 2013

### uknowwho

We just started out C++ and I thought of making a conversion program from binary to gray and vice versa just for fun and to improve my skills

I was successful to make conversions of 4 bits

#include <iostream>

using namespace std;

int main() {

int Choice;
bool g0,g1,g2,g3,b0,b1,b2,b3;

cout<<"For Binary To Gray Code Enter 1." << endl <<"For Gray Code to Binary Enter 2." << endl;;
cin>>Choice;

if(Choice==1){

cout<<"Enter the Binary Bits." << endl;

cin>>b0>>b1>>b2>>b3;

cout<<"Orginal Binary Form: "<<b3 <<b2 <<b1 <<b0 << endl;

g3=b3;
g2=b3^b2;
g1=b2^b1;
g0=b1^b0;

cout<<"Converted Gray Code Form: "<<g3 <<g2 <<g1 <<g0 << endl;
}
else if(Choice==2){

cout<<"Enter The Gray Code Bits." << endl;
cout<<"g0: ";
cin>>g0;
cout<<"g1: ";
cin>>g1;
cout<<"g2: ";
cin>>g2;
cout<<"g3: ";
cin>>g3;

cout<<"Orignal Gray Code Form: "<<g3 <<g2 <<g1 <<g0 << endl;

b3=g3;
b2=b3^g2;
b1=b2^g1;
b0=b1^g0;

cout<<"Converted Binary Form: "<<b3 <<b2 <<b1 <<b0 << endl;
}

return 0;
}

but what if I want to store the number of bits entered by the user in real time is it possible?
I heard using vectors is more useful in c++ than arrays? Also how to store the bits in a string so that the user can enter the bits in one line without spaces.
We haven't studied loops and making functions as of yet so it would be great If you suggest simpler ideas

2. Apr 21, 2013

### Staff: Mentor

Without loops and functions, you are limited to a fixed number of input bits (or an insane amount of if-statements), I think. You can use a string, but I don't see a reasonable method to tell C++ "do this [stringlength] times" without loops or functions.

>> Also how to store the bits in a string
In the same way you can store a single bit in a bool.

3. Apr 21, 2013

### Staff: Mentor

Code (Text):

#include <string>
#include <iostream>

using namespace std;

int main ()
{
string binarybits;
cout << "Enter the binary bits (without spaces): ";
cin >> binarybits;
cout << "OK, you entered " << binarybits << "." << endl;
return 0;
}

4. Apr 21, 2013

### uknowwho

Ok if we were to use loops what the simplist way to do it without getting to complicated

i know the basics of loops

5. Apr 21, 2013

### Solkar

This
is a valid requirement, but this
is based on an invalid assuption - you don't need a string for buffering the input when whitespaces are absent, pls consider this:

Code (Text):
#include <iostream>
#include <string>
#include <vector>
#include <cctype>    // iswspace
#include <stdexcept> // logic_error
#include <iterator>  // ostream_iterator
#include <algorithm> // std::copy

int main(/*int argc, char* argv[]*/) {
typedef std::vector<bool> bit_vec;
typedef std::ostream_iterator<bit_vec::value_type, char> outp_it;

bit_vec bv;
char c;

try {
while (std::cin.get(c)) {
if ((c == '0') || (c == '1')) {
bv.push_back(bool(c - '0'));
} else if (!iswspace(c)) {
throw std::domain_error(std::string(&c,1).c_str());
}
}
} catch (std::domain_error& e) {
std::cerr << "Invalid literal: " << e.what() << std::endl;
exit(1);
}

std::copy(bv.begin(), bv.end(), outp_it(std::cout));
std::cout << std::endl;

return 0;
}
You better downsize that yourself, from this
I cannot get what you're allowed to use - apparent free choice of containers but no loops or functions yet does not appear consistent.

Regards, Solkar

6. Apr 21, 2013

### uknowwho

No we haven'e learnt neither of those as of yet(vecors and arrays).I googled for those.

7. Apr 21, 2013

### Solkar

Nonsens, that's meant to be interactive, isnt it?
So scratch that and put
Code (Text):
while (std::cin.get(c) && c != '\n')