Thread: Help!!

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    Help!!

    i need help on one of my assignment.

    i have a program that can use arrow key to move up n down a choice of list.
    eg.
    1.login
    2.register
    3.exit
    it can highlight the choice n beep sound.

    the problem is, to do this i need to repeat certain things for it to highlight yhe choice when moving it up or down. n most of my other function is going to use this method. so, is there a way to simplify it?
    here is the source code:

    Code:
    #include "Header.h"
    
    int main()
    {
    	int ch,x,y;
    	HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    	WORD wOldColorAttrs;
    	CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
    	// First save the current color information
    	//get console scrren buffer info
    	GetConsoleScreenBufferInfo(h, &csbiInfo);
    	wOldColorAttrs = csbiInfo.wAttributes;
    	
        gotoxy(27,6);
        cout<<"**********************"<<endl;
        gotoxy(27,7);
        cout<<"*";
        gotoxy(48,7);
        cout<<"*";
        gotoxy(27,8);
        cout<<"*";
        gotoxy(48,8);
        cout<<"*";
    	gotoxy(30,8);
        //set the background colour of the word in to red colour
        SetConsoleTextAttribute ( h, BACKGROUND_BLUE | BACKGROUND_INTENSITY );
        cout << "1. Login.";
        //set the background colour of the word in to original colour
        SetConsoleTextAttribute ( h, wOldColorAttrs);
    	gotoxy(27,9);
        cout<<"*";
        gotoxy(48,9);
        cout<<"*";
        gotoxy(30,9);
        cout<<"2. Register";
        gotoxy(27,10);
        cout<<"*";
        gotoxy(48,10);
        cout<<"*";
        gotoxy(30, 10);
        cout<<"3. Exit";
        gotoxy(27,11);
        cout<<"*";
        gotoxy(48,11);
        cout<<"*";
        gotoxy(27,12);
        cout<<"**********************"<<endl;
        x = 30;
        y = 8;
      
        gotoxy(x,y);
        //start while loop
        while ( ( ch = get_code() ) != 27 ) 
        {//start switch
    		switch ( ch ) {
    		case 256 + 72://arrow key up
    			Beep(1568, 200);//sound
    			while (y <9)//start while loop
    				y = y +1;//end while loop
    			y = y-1;
    			gotoxy(x,y);
    			if (y == 8)//start if
    			{
    				gotoxy(30, 8);
    				//set the background colour of the word in to red colour
    				SetConsoleTextAttribute ( h, BACKGROUND_BLUE | BACKGROUND_INTENSITY );
    				cout << "1. Login.";
    				gotoxy(30, 9);
    				//set the background colour of the word in to original colour
    				SetConsoleTextAttribute ( h, wOldColorAttrs);
    				cout<<"2. Register";
    				gotoxy(30, 10);
    				cout<<"3. Exit";
    			}
    			else if (y == 9 )
    			{
    				gotoxy(30, 9);
    				//set the background colour of the word in to red colour
    				SetConsoleTextAttribute ( h, BACKGROUND_BLUE | BACKGROUND_INTENSITY );
    				cout<<"2. Register";
    				gotoxy(30, 8);
    				//set the background colour of the word in to original colour
    				SetConsoleTextAttribute ( h, wOldColorAttrs);
    				cout << "1. Login.";
    				gotoxy(30, 10);
    				cout<<"3. Exit";
    			}
    			else if (y == 10)
    			{
    				gotoxy(30,10);
    				//set the background colour of the word in to red colour
    				SetConsoleTextAttribute ( h, BACKGROUND_BLUE | BACKGROUND_INTENSITY );
    				cout<<"3. Exit.";
    				gotoxy(30,8);
    				//set the background colour of the word in to colours colour
    				SetConsoleTextAttribute ( h, wOldColorAttrs);
    				cout << "1. Login.";
    				gotoxy(30, 9);
    				cout<<"2. Register.";
    			}//end if
    		break;
        case 256 + 80://arrow key down
    		Beep(1568, 200);//sound
    		while (y >9)//start while loop
    			y = y -1;//end while loop
    		y = y+1;
    		gotoxy(x,y);
    		if (y == 8)//start if
    		{
    			gotoxy(30, 8);
    			//set the background colour of the word in to red colour
    			SetConsoleTextAttribute ( h, BACKGROUND_BLUE | BACKGROUND_INTENSITY );
    			cout << "1. Login.";
    			gotoxy(30, 9);
    			//set the background colour of the word in to original colour
    			SetConsoleTextAttribute ( h, wOldColorAttrs);
    			cout<<"2. Register.";
    			gotoxy(30, 10);
    			cout<<"3. Exit.";
    		}
    		else if (y == 9 )
    		{
    			gotoxy(30, 9);
    			//set the background colour of the word in to red colour
    			SetConsoleTextAttribute ( h, BACKGROUND_BLUE | BACKGROUND_INTENSITY );
    			cout<<"2. Register.";
    			gotoxy(30, 8);
    			//set the background colour of the word in to original colour
    			SetConsoleTextAttribute ( h, wOldColorAttrs);
    			cout << "1. Login.";
    			gotoxy(30, 10);
    			cout<<"3. Exit.";
    		}
    		else if (y == 10)
    		{
    			gotoxy(30,10);
    			//set the background colour of the word in to red colour
    			SetConsoleTextAttribute ( h, BACKGROUND_BLUE | BACKGROUND_INTENSITY );
    			cout<<"3. Exit.";
    			gotoxy(30,8);
    			//set the background colour of the word in to original colour
    			SetConsoleTextAttribute ( h, wOldColorAttrs);
    			cout << "1. Login.";
    			gotoxy(30, 9);
    			cout<<"2. Register.";
    		}//end if
    		break;
    	case 13://enter key
    		system("cls");//clear screen
    		if (y == 8)//start if
    			//call function Login
    			Login();
    		else if (y == 9)
    			//call function Regsiter
    			Register();
    		else if (y == 10)
    			//exit program
    			exit(-1);
    		//end if
    		}//end switch
    	}//end while
    	//set to old colour
    	SetConsoleTextAttribute ( h, wOldColorAttrs);
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    i just want to know if there is any way to shorten the coding. if there is non, then its ok.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could split up some of your code into functions (actually, you probably should).

    And I'm sure you can think of a better way to do this:
    Code:
    			while (y <9)//start while loop
    				y = y +1;//end while loop
    			y = y-1;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    Thanks for ur advise

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You could also rip out all the
    - gotoxy
    - system("cls");
    - SetConsoleTextAttribute
    And any other decorative functions until you've got the logic working correctly.

    Oh, and as already stated, use lots of functions to break up that monolithic main

Popular pages Recent additions subscribe to a feed