Hi, i'm new to this website. I have recently came across a qns given by a friend and i'm thinking how to do it. Hope u all can help me...

Here is the qns:

The circuit below shows a decoder circuit using 3-to-8 Decoder and 2-to-4 Decoder with logic gates.

*The Pic is in the attachment file.

Your task is to simulate the output logic level (ie: “0” or “1”) of each device in the above circuit. You will need to derive the following classes :

i) 3-to-8 Decoder
ii) 2-to-4 Decoder
iii) AND gate

Thank you for viewing and your help...

Recommended Answers

All 11 Replies

For a 2 input AND gate, you have 4 possible combination of inputs(0 to 3 in binary) . The output will only be a 1 if the two inputs are a 1, otherwise the output is a 0.

For a 2-4 bit decoder, you have an output for each combination of input. eg, If a=0 and b = 0 then D0= 0, or if a=0 and b=1 then D1=1 etc., just google the Truth Table for each chip. The 3-8 is the same with 3 inputs and 8 possible outputs...

This is a small program i have try to do but with errors... can anyone help me to solve up the error?

//Miniproject
#include <iostream>
using namespace std;
class decoder 
{
protected:
int decoderType;
public:
void setdecoderType(int); 
void showdecoder(void);
};
void decoder:: setdecoderType(int c)
{
decoderType = c;
}
void decoder::showdecoder()
{
switch (decoderType) 
{
case '000': cout << "0" << endl;
break;
case '001': cout << "1" << endl;
break;
case '010': cout << "2" << endl;
break;
case '011': cout << "3" << endl;
break;
case '100': cout << "4" << endl;
break;
case '101': cout << "5" << endl;
break;
case '110': cout << "6" << endl;
break;
case '111': cout << "7" << endl;
break; 
default: 
cout << "Wrong entry! Pls re-enter the 3 binary code!" << endl;
break;
}
}
 
 
 
void main()
{
Decode code;
int choice=0;
cout << "This is a 3 to 8 decoder. "; 
cout << "Please enter the 3 binary code!(E.g. 001) "; cin >> choice; 
code.setdecodertype(choice);
cout << "After decoding… The number is\n "; 
code.showdecoder();
}

Thanks for helping in advance...

First of all:
use int main() instead of void main()
please use code tags, it makes your code easier to read as does indention

With that said:

you have a few typo's in your code:

void main()
{
   Decode code;

Your class is called ' decoder ' not ' Decode', the line above to 'decoder code'

code.setdecodertype(choice);

There is no function 'setdecodertype'. Change this to 'setdecoderType'.

Please keep in mind that C++ is casesensitive and therefore: a!=A

This is a small program i have try to do but with errors... can anyone help me to solve up the error?

What error? We can't help it you don't tell us.

One of your coding mistake is as what nick stated. But there is also another mistake. Your coding is prompting the user for integer but in your switch case it is comparing integer with character. You should eliminate the single quote since it represent character. Then I think your coding should be working after you correct all the small mistakes. Hope it helps.

commented: Absolutly right, didn't see it. Have some rep on me! -Niek E +1

Thanks for your help for the previous errors...
Now i have rewrite the program but i still got some error and editing which need yours help.
Here is the code

//Miniproject
 
#include <iostream>
using namespace std;
 
class decoder 
{
protected:
      int decoderType;
public:
       void setdecoderType(int);    
     void showdecoder(void);
};
 
void decoder:: setdecoderType(int c)
{
     decoderType = c;
}
 
void decoder::showdecoder()
{
      switch (decoderType) 
      {
        case 000: cout << "0" << endl;
                  break;
            case 001: cout << "1" << endl;
                  break;
            case 010: cout << "2" << endl;
                  break;
        case 011: cout << "3" << endl;
                  break;
        case 100: cout << "4" << endl;
                  break;
        case 101: cout << "5" << endl;
                  break;
        case 110: cout << "6" << endl;
                  break;
        case 111: cout << "7" << endl;
                  break;      
            default:  
                  cout << "\nWrong entry! Pls re-enter the 3 binary code!" << endl;
                  break;
      }
}
 
class twofour 
{
protected:
      int twofourType;
public:
       void settwofourType(int);    
     void showtwofour(void);
};
 
void twofour:: settwofourType(int c)
{
     twofourType = c;
}
 
void twofour::showtwofour()
{
      switch (twofourType) 
      {
        case 00: cout << "0" << endl;
                  break;
            case 01: cout << "1" << endl;
                  break;
            case 10: cout << "2" << endl;
                  break;
        case 11: cout << "3" << endl;
                  break;
            default:  
                  cout << "\nWrong entry! Pls re-enter the 2 binary code!" << endl;
                  break;
      }
}
 
class and 
{
protected:
      int andType;
public:
       void setandType(int);        
     void showand(void);
};
 
void and:: setandType(int c)
{
     andType = c;
}
 
void and::showand()
{
      switch (andType) 
      {
        case 00: cout << "0" << endl;
                  break;
            case 01: cout << "0" << endl;
                  break;
            case 10: cout << "0" << endl;
                  break;
        case 11: cout << "1" << endl;
                  break;
            default:  
                  cout << "\nWrong entry! Pls re-enter the 2 binary code!" << endl;
                  break;
      }
}
 
void main()
{
   decoder code;
twofour two;
and gate;
   int  choice=0;
 
   cout << "This is a 3 to 8 decoder.\n";      
   cout << "Please enter the 3 binary code!(E.g. 001 to 111) "; cin >> choice; 
   code.setdecoderType(choice);
   cout << "After decoding, the number is: ";   
   code.showdecoder();
 
cout << "\nThis is a 2 to 4 decoder.\n";      
   cout << "Please enter the 2 binary code!(E.g. 00,01,10,11) "; cin >> choice; 
   two.settwofourType(choice);
   cout << "After decoding, the number is: ";   
   two.showtwofour();
 
cout << "\nThis is a And Gate.\n";      
   cout << "Please enter the 2 binary combination!(E.g. 00,01,10,11) "; cin >> choice; 
   gate.setandType(choice);
   cout << "The result is: ";   
   gate.showand();
}

First problem - For the 3 to 8 decoder, when 011 and 001 was keyed, it shows error instead of 3 and 1.

Second problem - how can i edit the program so the users can choose to use either one of the 3 decoder? I have try alot of times but all got errors.

Third problem - How can i do loop back if i key in a wrong value and i want to key in the value again after the error line has been shown?

Thanks...

usingnamespace std;

There should be a space between using and namespace

First problem - For the 3 to 8 decoder, when 011 and 001 was keyed, it shows error instead of 3 and 1.

That's a problem with int's. 011 == 11 and is an invalid input. 010 will probably have the same problem. Try making the input to string and switch on that.
Or if you want to do it the ugly way: replace 011 with 11 and 010 with 10 in your switchcase.

Second problem - how can i edit the program so the users can choose to use either one of the 3 decoder? I have try alot of times but all got errors.

That's not that hard is it? Ask for a user input (1 for 3-8, 2 for etc.) Make a switch case on that and execute the right function.

Third problem - How can i do loop back if i key in a wrong value and i want to key in the value again after the error line has been shown?

you should work with returnvalues in your function. So if the input was valid return '0' and if it was invalid return '1'. Then call the function in a loop.

That's a problem with int's. 011 == 11 and is an invalid input. 010 will probably have the same problem. Try making the input to string and switch on that.

Actually, 011 = 9. When you specify a numeric value with a leading 0, you are using an [search]octal[/search] value. You can't specify binary the way you are trying to do it. Enter it as a string and convert the string into it's binary equivalent. And in the switch, just use it's decimal value.

Actually, 011 = 9. When you specify a numeric value with a leading 0, you are using an octal

Didn't know that, I thought (int)011 == (int)11. I've tested it in flamecly's program (by replacing 011 with 11 in the switch case) and it works as I'd expect: if user inputs 011, case 11 will be true.
How is that then? Perhaps 'cin' doesn't know how to handle octals?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.