Binary to Gray Conversion Program

  • Thread starter Thread starter uknowwho
  • Start date Start date
AI Thread Summary
The discussion revolves around creating a C++ program for converting binary to Gray code and vice versa. The initial code successfully handles 4-bit conversions, prompting the user for input and displaying the results. A key inquiry is how to store an arbitrary number of bits entered by the user in real-time, with suggestions leaning towards using strings or vectors. However, the constraints of not using loops or functions complicate the implementation, leading to concerns about fixed input limits or excessive conditional statements. Participants explore the idea of using strings for input without spaces, highlighting that a string can effectively store bits. There is also a suggestion to utilize vectors, which are considered more flexible than arrays. The conversation touches on the limitations of the current knowledge base, as loops and functions have not yet been studied, prompting discussions on simpler alternatives. Ultimately, the need for a method to handle input dynamically while adhering to the current learning constraints is emphasized, with some participants expressing confusion over the allowed programming constructs.
uknowwho
Messages
25
Reaction score
0
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
 
Technology news on Phys.org
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.
 
uknowwho said:
how to store the bits in a string so that the user can enter the bits in one line without spaces

Code:
#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;
}
 
mfb said:
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.

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

i know the basics of loops
 
uknowwho said:
Also how to store the bits in a string so that the user can enter the bits in one line without spaces.

This
user can enter the bits in one line without spaces
is a valid requirement, but this
Also how to store the bits in a string
is based on an invalid assuption - you don't need a string for buffering the input when whitespaces are absent, pls consider this:

Code:
#include <iostream>
#include <string>   
#include <vector>
#include <cctype>    // iswspace
#include <stdexcept> // logic_error
#include <iterator>  // ostream_iterator 
#include <algorithm> // std::copyint 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
uknowwho said:
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
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
 
Solkar said:
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:
#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: " << c << 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

No we haven'e learned neither of those as of yet(vecors and arrays).I googled for those.
 
Solkar said:
Code:
 while (std::cin.get(c))
Nonsens, that's meant to be interactive, isn't it?
So scratch that and put
Code:
 while (std::cin.get(c) && c != '\n')
 
Back
Top