Thread: Need help on program that alters the led combination with different input

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    1

    Need help on program that alters the led combination with different input

    Hi guys i'm new to the forum and i currently have a problem with my program. I'm confused about how to make my program work. The code below shows what i have done up to this stage. [input]Whenever the user input 0 0 the led pattern remains unchange. when 0 1 is input, the led pattern is rotated right by 1 led position. when 1 0 is input the led pattern is rotated left by 1 led position. when 1 1 is input. only the 5th led from the left is lighted up.[/input]
    Can anyone please help me? Please tell me if i missed out on any info.
    Code:
    #include <iostream>
    using namespace std;
    
    class Switch
    {
    protected:
    int sw1, sw2;
    public:
    void setSwitch(int a,int b);
    void getSwitch();
    };
    
    void Switch::setSwitch(int a,int b)
    {
    	sw1 = a;
    	sw2 = b;
    }
    
    void Switch::getSwitch()
    {
    	cout<<"Enter logic states of sw1 and sw0, seperated by a space:";
    	cin>>sw1;
    
    	switch(sw1){
           case 00:cout<<"Enter logic states of sw1 and sw0, seperated by a space:"<<endl;
    			cout<<"Case1"<<endl;
    			  break;
           case 01:cout<<"Enter logic states of sw1 and sw0, seperated by a space:"<<endl;
    		   cout<<"Case2"<<endl;
    		      break;
           case 10:cout<<"Enter logic states of sw1 and sw0, seperated by a space:"<<endl;
    		   cout<<"Case3"<<endl;
    			  break;
           case 11:cout<<"Enter logic states of sw1 and sw0, seperated by a space:"<<endl;
    		   cout<<"Case4"<<endl;
    		      break;
    		      default: cout<<"Wrong Input"<<endl;
    		      break;
    	}
    
    }
    
    class Led
    {
    protected:
    int led[8];
    public:
    	Led();
    void showLed();
    void LED3();
    };
    Led::Led()
    {
    	for(int i=0;i<8;i++)
    		led[i]=0;
    	led[7]=1;
    }
    
    void Led::showLed()
    {
    	int i;
    	cout<<"Led Pattern: ";
    	for (i=0;i<8;i++)
    		cout<<" "<<led[i];
    	cout<<endl;
    }
    
    
    
    void Led::LED3()
    {
    	int i;
    	cout<<"Led Pattern: ";
    	for(int i=0;i<8;i++)
    		cout<<" "<<led[i];
    }
    
    class Circuit : public Switch, public Led
    {
    public:
    void showcircuit();
    void rotateleft();
    void rotateright();
    };
    void showcircuit();
    
    void Circuit::rotateleft()
    {
    	int b;
    b=led[7];
    led[7]=led[6];
    led[6]=led[5];
    led[5]=led[4];
    led[4]=led[3];
    led[3]=led[2];
    led[2]=led[1];
    led[1]=led[0];
    led[0]=b;
    int i;
    	cout<<"Led Pattern: ";
    	for (i=0;i<8;i++)
    		cout<<" "<<led[i];
    	cout<<endl;
    
    }
    void Circuit::rotateright()
    {
    	int a;
    a=led[0];
    led[0]=led[1];
    led[1]=led[2];
    led[2]=led[3];
    led[3]=led[4];
    led[4]=led[5];
    led[5]=led[6];
    led[6]=led[7];
    led[7]=a;
    int i;
    	cout<<"Led Pattern: ";
    	for (i=0;i<8;i++)
    		cout<<" "<<led[i];
    	cout<<endl;
    
    }
    main()
    {
    	Circuit Circuits;
    	Circuits.showLed();
    	Circuits.getSwitch();
    	Circuits.rotateright();
    	Circuits.rotateleft();
    	Circuits.LED3();
    }

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Is there any specific reason why you are overcomplicating this so much? Read
    what I said in this thread about member functions, and also see the link in my
    sig about main.

    Also,

    Code:
    cout<<"Enter logic states of sw1 and sw0, seperated by a space:";
    	cin>>sw1;
    If you do that, you'll only read in one of the values, and the other digit will be
    left in the input buffer - not good. I assume that you're looking for this:

    cin>>sw1>>sw2;

    Also, what's with that switch statement? Seems a little useless to me. Also,
    it looks like you want the cases to be looking at both digits entered - you can't
    do that with a switch statement if both digits are stored in separate variables.

    Lastly, what's the point of the LED3 function when the showled function does
    the same thing?


    You have an unused variable:
    Code:
    int i; //either get rid of this, or
    cout<<"Led Pattern: ";
    for(int i=0;i<8;i++) // remove the keyword "int" from this line
    but that's the least of your troubles.

    Lastly, your indentation isn't great - one word - consistency - that's all you need.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. getting input from another program
    By adr in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2006, 03:29 AM
  4. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM