Binary to Gray Conversion Program

  • Thread starter Thread starter uknowwho
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around creating a C++ program for converting binary numbers to Gray code and vice versa. Participants explore how to handle user input for binary bits, particularly focusing on storing bits in a string format without spaces and the limitations of not using loops or functions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant shares a basic C++ program for converting binary to Gray code and seeks advice on improving user input handling.
  • Another participant suggests that without loops and functions, the program would be limited to a fixed number of bits or require excessive conditional statements.
  • A different participant provides a code snippet demonstrating how to read binary bits as a string without spaces.
  • Some participants discuss the validity of using strings for input buffering and propose alternatives using vectors.
  • There is a suggestion to simplify the input process if loops were allowed, indicating that the participant has basic knowledge of loops.
  • One participant questions the consistency of the restrictions on using containers like vectors while not being allowed to use loops or functions.
  • Another participant critiques the interactive nature of the input method suggested in a provided code snippet, proposing a modification for better user interaction.

Areas of Agreement / Disagreement

Participants express differing views on the best method for handling user input and the implications of the restrictions on using loops and functions. There is no consensus on the optimal approach to take given the constraints.

Contextual Notes

Participants have not yet learned about vectors and arrays, which may limit their ability to implement certain suggestions. The discussion reflects uncertainty regarding the requirements and capabilities of the programming task.

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;

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

if(Choice==1){

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

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


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

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

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

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

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

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

count<<"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')
 

Similar threads

Replies
235
Views
15K
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 14 ·
Replies
14
Views
35K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K