[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

!!!

Posted on 2004-10-28
6
Medium Priority
?
240 Views
Last Modified: 2010-04-01
Hey dudes,

I got this question that i am working on currently. But, having problems.. I just need help on the errors. and how to fix them.

Here is the question: -------------------------------------------------------------------------------------------------------------

Design an object oriented program which computes and returns the output (0 or 1) for the AND, OR, NAND and NOR gate respectively.
Create a Gate class that contains the following members:
-data members to store two inputs and an output of a gate
-four inline member functions that compute and return the output for the AND, OR, NAND and NOR gate respectively  
-a non-inline function that displays menu options (types of gates available) when choosing a type of the gate
-a non-inline function that gets two inputs of the gate from the user
-a constructor function that initializes two inputs of the gate

SAMPLE OUTPUT LOOKS LIKE THIS:

               Types of gates available:
1.      AND
2.      OR
3.      NAND
4.      NOR

 
                 Choose the type of gate for gate 1 => 1
                 Enter two inputs for gate 1 => 0 1
 
                 Choose the type of gate for gate 2 => 3
                 Enter two inputs for gate 2 => 1 0

                 Choose the type of gate for gate 3 => 2

                  Outputs =>
                                      y1 = 0
                                      y2 = 1
                                      y3 = 1

-----------------------------------------------------------------------------------------------------------------------------------------


HERE IS MY CODE:

#include <iostream>
#include <stdlib.h>
#include<conio.h>

using namespace std;


class Gate {

private:

int x1, x2, y;

public:
int op_and(int x1 , int x2)
{
return(x1&x2);
}

int op_or(int x1, int x2)
{
return (x1|x2);
}

int op_nand(int x1, int x2)
{
return !(x1&x2);
}

int op_nor(int x1, int x2)
{
return !(x1|x2);
}

Gate (int in1, int in2);
void display ();
void getinput(int x, int y);
int schoice (int choice, int x, int y);

};

void Gate::display()
{
cout<<"Types of gates available: "<<endl;
      cout<<"1. AND"<<endl;
      cout<<"2. OR"<<endl;
      cout<<"3. NAND"<<endl;
      cout<<"4. NOR"<<endl;
      cout<<endl;

        }
       
void Gate::getinput(int x1, int x2) {

    cout<<"Enter two inputs for gate: "<<x1<<x2<<endl;
   
}

Gate::Gate (int in1, int in2) {
   
      in1 = x1;
      in2 = x2;
  }    

int schoice (int choice, int in1, int in2){
   
    int x1, x2, x3, x4;
    Gate (in1, in2);
    switch(choice)
    {
        case 1: x1 = op_and(in1,in2);
                return x1;
        break;
        case 2: x2 = op_or(in1,in2);
                return x2;
        break;
        case 3: x3 = op_nand(in1,in2);
                return x3;
        break;
        case 4: x4 = op_nor(in1,in2);
                return x4;
        break;
        default:
            cout<<"wrong entry";
           
}    
           


int main()
{
 int choice,x1, x2, x3,x4, gate_out1, gate_out2, gate_out3 ,x,y,y1, y2, y3;
    Gate gate;
       
    cout<<"Choose the type of gate for gate 1: "<<endl;
      cin>>choice;
      gate.getinput();
      gate.y = gate.schoice(choice, in1, in2);
      cout<<endl;
      cout<<"Enter the type of gate for gate 2: "<<endl;
      cin>>choice;
      cout<<"Enter two inputs for gate 2: "<<endl;
/*      cin>> x>>y;
      y2 = schoice(choice, x, y);
      cout<<endl;
      cout<<"Enter the type of gate for gate 3: "<<endl;
      cin>>choice;
      cout<<"Enter two inputs for gate 3: "<<endl;
      cin>> x>>y;
      cout<<endl;
      y3 = schoice(choice, x, y);
      cout<< "Outputs:"<<endl;
      cout<< "y1= "<<y1<<endl;
      cout<<"y2= "<<y2<<endl;
      cout<<"y3= "<<y3<<endl;
      cout<<endl<<endl;
      //code for the 3-AND gate network--------------------
      cout<<" -----3-AND gate Network------"<<endl<<endl;
      cout<<"Enter 2 inputs for gate 1:" <<endl;
      cin>> x1>>x2;
      cout<<"Enter 2 inputs for gate 2:" <<endl;
      cin>> x3>>x4;
      gate_out1 = (x1&x2);
      gate_out2 = (x3&x4);
      gate_out3 = (gate_out1)&(gate_out2);
      cout<<endl;
      cout<<"Gate 1 output is:"<<gate_out1<<endl;
      cout<<"Gate 2 output is:"<<gate_out2<<endl;
      cout<<"Gate 3 output is:"<< gate_out3<<endl; */
      getch();
      return 0;
}

-------------------------------------------------------------------------------------------------------------------------------

I got whole bunch of INITIAL ERROS on the op_and function, and the rest..that they haven't been declared etc..

Thanks
Dino.
0
Comment
Question by:dinorama
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12440233
OK, the schoice function is a class function so it should be defined as a member of class
U have declared it as a member, but while defining, u are defininfg it as a standalone function


So i changed this line in your program
int schoice (int choice, int in1, int in2){
to
int Gate::schoice (int choice, int in1, int in2){


2.

A closing brace is missing for the switch statement in schoice


3.

In main, u have declared a Gate variable passing no parameters to constructir
Since u have declared one constructor taking parameter, u should define a no argument [or construcor with all arguments having default values] constructor to define a variable without requiring to pass parameters.

Amit
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 300 total points
ID: 12440237
One more logical error that I see in your program is
Gate::Gate (int in1, int in2) {
   
      in1 = x1;
      in2 = x2;
  }  

Here, u are assigning the values in x and y to the arguments, but actually u should be doing the other way round
Hence it should be

Gate::Gate (int in1, int in2) {
   
      x1 = in1 ;
      x2 = in2 ;
  }
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12440247
also, to be able to create an instence of Gate class without passing any parameters,
U could write the above constructor as

Gate::Gate (int in1 = 0, int in2 = 0) {
   
      x1 = in1 ;
      x2 = in2 ;
  }

so that by default the x and y inputs get initialized to 0 if the user does not pass aything.

Amit
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:jkr
ID: 12440294
Your code seems to have a lot of misconceptions. I tried to fix some (it compiles at least :o), but it is still up to you to finish it:

#include <iostream>
#include <stdlib.h>
#include<conio.h>

using namespace std;


class Gate {

private:

int x1, x2, y;

public:
int op_and(int x1 , int x2)
{
return(x1&x2);
}

int op_or(int x1, int x2)
{
return (x1|x2);
}

int op_nand(int x1, int x2)
{
return !(x1&x2);
}

int op_nor(int x1, int x2)
{
return !(x1|x2);
}
Gate() {};
Gate (int in1, int in2);
void display ();
void getinput();
int schoice (int);

};

void Gate::display()
{
cout<<"Types of gates available: "<<endl;
    cout<<"1. AND"<<endl;
    cout<<"2. OR"<<endl;
    cout<<"3. NAND"<<endl;
    cout<<"4. NOR"<<endl;
    cout<<endl;

       }
       
void Gate::getinput() {

   cout<<"Enter two inputs for gate: ",
   cin>>x1>>x2;
   
}

Gate::Gate (int in1, int in2) {
   
     x1 = in1;
     x2 = in2;
 }    

int Gate::schoice (int choice){
   
   int x;
   
   switch(choice)
   {
       case 1: x = op_and(x1,x2);
               return x;
       break;
       case 2: x = op_or(x1,x2);
               return x;
       break;
       case 3: x = op_nand(x1,x2);
               return x;
       break;
       case 4: x = op_nor(x1,x2);
               return x;
       break;
       default:
           cout<<"wrong entry";
   }
}    
           


int main()
{
int choice,x1, x2, x3,x4, gate_out1, gate_out2, gate_out3 ,x,y,y1, y2, y3;
   Gate gate;
     
   cout<<"Choose the type of gate for gate 1: "<<endl;
    cin>>choice;
    gate.getinput();
    y = gate.schoice(choice);
    cout<<endl;
    cout<<"Enter the type of gate for gate 2: "<<endl;
    cin>>choice;
    cout<<"Enter two inputs for gate 2: "<<endl;
/*     cin>> x>>y;
    y2 = schoice(choice, x, y);
    cout<<endl;
    cout<<"Enter the type of gate for gate 3: "<<endl;
    cin>>choice;
    cout<<"Enter two inputs for gate 3: "<<endl;
    cin>> x>>y;
    cout<<endl;
    y3 = schoice(choice, x, y);
    cout<< "Outputs:"<<endl;
    cout<< "y1= "<<y1<<endl;
    cout<<"y2= "<<y2<<endl;
    cout<<"y3= "<<y3<<endl;
    cout<<endl<<endl;
    //code for the 3-AND gate network--------------------
    cout<<" -----3-AND gate Network------"<<endl<<endl;
    cout<<"Enter 2 inputs for gate 1:" <<endl;
    cin>> x1>>x2;
    cout<<"Enter 2 inputs for gate 2:" <<endl;
    cin>> x3>>x4;
    gate_out1 = (x1&x2);
    gate_out2 = (x3&x4);
    gate_out3 = (gate_out1)&(gate_out2);
    cout<<endl;
    cout<<"Gate 1 output is:"<<gate_out1<<endl;
    cout<<"Gate 2 output is:"<<gate_out2<<endl;
    cout<<"Gate 3 output is:"<< gate_out3<<endl; */
    getch();
    return 0;
}
0
 
LVL 86

Accepted Solution

by:
jkr earned 1200 total points
ID: 12440414
BTW, some more improvements - you are passing member variables of your 'Gate' when you don't have to, especially with the 'op_*()' functions:

class Gate {

private:

int x1, x2, y;

public:
int op_and()
{
return(x1&x2);
}

int op_or()
{
return (x1|x2);
}

int op_nand()
{
return !(x1&x2);
}

int op_nor()
{
return !(x1|x2);
}
Gate() {};
Gate (int in1, int in2);
void display ();
void getinput();
int schoice (int);

};

void Gate::display()
{
cout<<"Types of gates available: "<<endl;
    cout<<"1. AND"<<endl;
    cout<<"2. OR"<<endl;
    cout<<"3. NAND"<<endl;
    cout<<"4. NOR"<<endl;
    cout<<endl;

       }
       
void Gate::getinput() {

   cout<<"Enter two inputs for gate: ",
   cin>>x1>>x2;
   
}

Gate::Gate (int in1, int in2) {
   
     x1 = in1;
     x2 = in2;
 }    

int Gate::schoice (int choice){
   
   int x;
   
   switch(choice)
   {
       case 1: x = op_and();
               return x;
       break;
       case 2: x = op_or();
               return x;
       break;
       case 3: x = op_nand();
               return x;
       break;
       case 4: x = op_nor();
               return x;
       break;
       default:
           cout<<"wrong entry";
   }
}    
0
 

Author Comment

by:dinorama
ID: 12448560
thanks to everybody who helped me out. I finished my assignment successfully!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question