Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Binary_Gray Conversion

  1. Apr 21, 2013 #1
    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. jcsd
  3. Apr 21, 2013 #2

    mfb

    User Avatar
    2016 Award

    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.
     
  4. Apr 21, 2013 #3

    jtbell

    User Avatar

    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;
    }
     
     
  5. Apr 21, 2013 #4
    Ok if we were to use loops what the simplist way to do it without getting to complicated

    i know the basics of loops
     
  6. Apr 21, 2013 #5
    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
     
  7. Apr 21, 2013 #6
    No we haven'e learnt neither of those as of yet(vecors and arrays).I googled for those.
     
  8. Apr 21, 2013 #7
    Nonsens, that's meant to be interactive, isnt it?
    So scratch that and put
    Code (Text):
     while (std::cin.get(c) && c != '\n')
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Binary_Gray Conversion
  1. Base conversion in C (Replies: 8)

Loading...