Notepad Code Problem ???

This is a discussion on Notepad Code Problem ??? within the C Programming forums, part of the General Programming Boards category; Please Please Please Help me, gotoxy() along with the weird working of backspace is driving me crazy ... the comments ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    11

    Notepad Code Problem ???

    Please Please Please Help me, gotoxy() along with the weird working of backspace is driving me crazy ... the comments are given with the stepts where I am falling ...

    BTW ... this is the code for a notepad ...

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <dos.h>
    #include <fcntl.h>
    #include <stdlib.h>
    
    
    int 	x,y;
    //**************************************************************************
    //                             Constants Block
    //**************************************************************************
    enum xKEYS {
    		L_ARROW = 75 ,
    		R_ARROW = 77 ,
    		U_ARROW = 72 ,
    		D_ARROW = 80 ,
    		ALT_O   = 24 ,
    		ALT_N   = 49 ,
    		ALT_S   = 31 ,
    		ALT_Q   = 16 ,
    		ALT_H   = 34 ,
    		ALT_C   = 47 ,
    		F4      = 62 ,
    		};
    
    enum SCREENSTATS{
    			MAXX = 80      ,
    			MAXY = 24      ,
    			AREAX = MAXX-1 ,
    			AREAY = MAXY-1 ,
    			MIDX = MAXX/2  ,
    			MIDY = MAXY/2  ,
    		};
    
    
    //************************************************************************
    //                         End of Constant Block
    //************************************************************************
    
    
    
    //************************************************************************
    //                         Function ProtoTypes
    //************************************************************************
    
    int open_file();
    int create_new_file();
    int _Row(int limit,char msgs[],int pos1,char msgs2[],int pos2,char msgs3[],int pos3);
    
    
    //************************************************************************
    //                     End of Function ProtoType Block
    //************************************************************************
    
    
    
    //************************************************************************
    //		           main() Program
    //************************************************************************
    int main()
    {
    
    	//*****************************/
    	//         VARAIABLE block
    	//*****************************/
    	int		key;
    	int		c;
    	char*	msgs[] = {
    				 "Alt + N (NewFile)"
    				,"Alt + O (Open)"
    				,"Alt + S (Save)"
    				,"Alt + H (Help)"
    				,"Alt + C (Close)"
    				,"Alt + Q (Quit)"
    				,"\nDo you want to save the Document"
    				,"\nDo you wish to contiue "
    				,"\n Y for Yes and N for No"
    
    			 };
    
    	int keysize;
    
    	keysize=(sizeof msgs) / (sizeof msgs[0]);
    	x=y=0;
    
    	clrscr();
    
    	textmode(3);
    
    	 gotoxy(0,0);
    	 _Row(40,msgs[0],4,msgs[1],15,msgs[2],24);
    
    	 gotoxy(MAXX,MAXY);
    	 _Row(40,msgs[3],4,msgs[4],15,msgs[5],24);
    
    	textcolor(LIGHTGREEN);
    	textbackground(BLACK);
    	x=0,y=1;
      gotoxy(x,y);    // Here's problem-1: the cursor isnt returning as it supposed to the pointed location
      while( (key=getch()) != 27 )
    	if( key == 0 )
    		switch(getch())  // Incomplete Part ... Skip This whole Block
    		{
    			case L_ARROW:
    				   //if(x>0)
    				gotoxy(--x,y);
    				break;
    			case R_ARROW:
    				//if(x<MAXX)
    				gotoxy(++x,y);
    				break;
    			case U_ARROW:
    				//if(y>0)
    				gotoxy(x,--y);
    				break;
    			case D_ARROW:
    				gotoxy(x,++y);
    				break;
    			case ALT_S:
    				printf(msgs[6]);
    				break;
    			case F4:
    				exit(0);
    
    	}
    	else
    	{
    	 if(key == 0x0a || key == 0x0d)
    		cputs("\n");
    	 if(key == 0x08)  // Backspace --- BIGGEST PROBLEM FOR ME, NOT SOLVED YET
    	 {
    		gotoxy(--(x=wherex()),(y=wherey()));
    		key = 0;
    	 }
    	  cprintf("%c",key);
    	  x=wherex();
    	  y=wherey();
    	 }
    
      return EXIT_SUCCESS;
    }
    
    //************************************************************************
    //			   Function Definations
    //************************************************************************
    
    int newfile(char* fname,int key)
    {
       FILE* in;
    	  if ((in = fopen(fname, "w+")) == NULL)
    	  {
    		fprintf(stderr, "Cannot create new file.\n");
    		return 1;
    	  }
    
    }
    
    int _Row(int limit,char msgs[],int pos1,char msgs2[],int pos2,char msgs3[],int pos3)
    {
      int c;
    		for(c=0; c < limit ;c++)
    		{
    			textcolor(BLACK);
    			textbackground(WHITE);
    
    			if( c == pos1 )
    				{
    					textcolor(RED);
    					textbackground(WHITE);
    					cprintf(msgs);
    				}
    			else if( c == pos2 )
    				{
    					textcolor(RED);
    					textbackground(WHITE);
    					cprintf(msgs2);
    				}
    			else if( c == pos3 )
    				{
    					textcolor(RED);
    					textbackground(WHITE);
    					cprintf(msgs3);
    				 }
    			else
    				{
    					y=wherey();
    					  if(y != 2)
    						cputs(" ");
    					  else
    						break;
    				 }
    		}
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > gotoxy(--(x=wherex()),(y=wherey()));
    I have NO idea what that evaluates to, and I bet you don't either.
    Just expand it out into easy to understand steps.

    Also, since you mention gotoxy(), have you read the FAQ on the subject?
    Perhaps you're using a compiler for the modern era.

    Also, try to indent your code more sensibly - it's a raggy mess when posted on the board. This is a big turn-off for most people.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    11
    Salem's Post :
    > gotoxy(--(x=wherex()),(y=wherey()));
    I have NO idea what that evaluates to, and I bet you don't either.
    Just expand it out into easy to understand steps.
    in this part by the help of gotoxy(), I'm first looking at the current location of x and y and then moving one space back on x-axis ... but as you said to expand it, I did it in the program quoted below ...

    Also, since you mention gotoxy(), have you read the FAQ on the subject?
    Perhaps you're using a compiler for the modern era.
    No, I'm using a quite older complier, I'm using Turbo C/C++ v.3.1

    Also, try to indent your code more sensibly - it's a raggy mess when posted on the board. This is a big turn-off for most people.
    I rearranged the code quite a bit, and here's the code again indented and with the expansion of gotoxy(), I'm not sure if the indentation is accordint to your taste or not but see it if it helps :

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <dos.h>
    #include <fcntl.h>
    #include <stdlib.h>
    
    
    int 	x,y;
    
    // Constants Block
    
    enum xKEYS {
    		L_ARROW = 75 ,
    		R_ARROW = 77 ,
    		U_ARROW = 72 ,
    		D_ARROW = 80 ,
    		ALT_O   = 24 ,
    		ALT_N   = 49 ,
    		ALT_S   = 31 ,
    		ALT_Q   = 16 ,
    		ALT_H   = 34 ,
    		ALT_C   = 47 ,
    		F4      = 62 ,
    		};
    
    enum SCREENSTATS{
    			MAXX = 80      ,
    			MAXY = 24      ,
    			AREAX = MAXX-1 ,
    			AREAY = MAXY-1 ,
    			MIDX = MAXX/2  ,
    			MIDY = MAXY/2  ,
    		};
    
    
    
    
    
    //  Function ProtoTypes
    
    
    int open_file();
    int create_new_file();
    int _Row(int limit,char msgs[],int pos1,char msgs2[],int pos2,char msgs3[],int pos3);
    
    
    
    
    
    // main() Program
    
    int main()
    {
    
    	// VARAIABLES
    	int		key;
    	int		c;
    	char*	msgs[] = {
    				 "Alt + N (NewFile)"
    				,"Alt + O (Open)"
    				,"Alt + S (Save)"
    				,"Alt + H (Help)"
    				,"Alt + C (Close)"
    				,"Alt + Q (Quit)"
    				,"\nDo you want to save the Document"
    				,"\nDo you wish to contiue "
    				,"\n Y for Yes and N for No"
    
    			 };
    
    	int keysize;
    
    	keysize=(sizeof msgs) / (sizeof msgs[0]);
    	x=y=0;
    
    	clrscr();
    	textmode(3);
    
    	 gotoxy(0,0);
    	        _Row(40,msgs[0],4,msgs[1],15,msgs[2],24);
    
    	 gotoxy(MAXX,MAXY);
    			_Row(40,msgs[3],4,msgs[4],15,msgs[5],24);
    
    	textcolor(LIGHTGREEN);
    	textbackground(BLACK);
    	
    	x=0,y=1;
         gotoxy(x,y);    
    	 /*
    		The problem is with the above gotoxy()
    		It is not returning to the location (0,1)
    		inspite it remains at the last line	   
    	 */
    
      while( (key=getch()) != 27 )
    	if( key == 0 )
    		switch(getch())  // Incomplete Part ... Skip This whole Block
    		{
    			case L_ARROW:
    				   //if(x>0)
    				gotoxy(--x,y);
    				break;
    			case R_ARROW:
    				//if(x<MAXX)
    				gotoxy(++x,y);
    				break;
    			case U_ARROW:
    				//if(y>0)
    				gotoxy(x,--y);
    				break;
    			case D_ARROW:
    				gotoxy(x,++y);
    				break;
    			case ALT_S:
    				printf(msgs[6]);
    				break;
    			case F4:
    				exit(0);
    
    	}
    	else
    	{
    	   if(key == 0x0a || key == 0x0d)
    		    cputs("\n");
    	   if(key == 0x08)  // Backspace --- BIGGEST PROBLEM FOR ME, NOT SOLVED YET
    	   {
    		    x=wherex();    // For the current position of x
    		    y=wherey();    // For the current position of y
    		    gotoxy(--x,y); // Move one position back as backspace is pressed
    		    key = 0;
    	   }
    	  cprintf("%c",key);
    	  x=wherex();
    	  y=wherey();
    	 }
    
      return EXIT_SUCCESS;
    }
    
    //  Function Definations
    
    int newfile(char* fname,int key)
    {
       FILE* in;
    	  if ((in = fopen(fname, "w+")) == NULL)
    	  {
    		fprintf(stderr, "Cannot create new file.\n");
    		return 1;
    	  }
    
    }
    
    int _Row(int limit,char msgs[],int pos1,char msgs2[],int pos2,char msgs3[],int pos3)
    {
      int c;
    		for(c=0; c < limit ;c++)
    		{
    			textcolor(BLACK);
    			textbackground(WHITE);
    
    			if( c == pos1 )
    				{
    					textcolor(RED);
    					textbackground(WHITE);
    					cprintf(msgs);
    				}
    			else if( c == pos2 )
    				{
    					textcolor(RED);
    					textbackground(WHITE);
    					cprintf(msgs2);
    				}
    			else if( c == pos3 )
    				{
    					textcolor(RED);
    					textbackground(WHITE);
    					cprintf(msgs3);
    				 }
    			else
    				{
    					y=wherey();
    					  if(y != 2)
    						cputs(" ");
    					  else
    						break;
    				 }
    		}
    }
    Last edited by keats; 06-17-2005 at 12:40 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    keats,

    I tried to compile your stuff on an old BCC 3.1 (1992!) that I found on my hard disk (with Windows XP) and I had the same problems.
    If you remove gotoxy(MAXX, MAXY) and remove the following _Row(), the cursor seems to work...

    Maybe the Windows Console cannot emulate old DOS so well?

    If I were you, I would convert to some (free) 32-bit compiler and I would use NCURSES for Windows as Text User Interface Library. It is almost compatible with Borland...

    Best regards, bilbo

  5. #5
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,266
    That's all well and good bilbo, but ncurses for Windows doesn't exist (seems no-one attached to that project can be bothered to try to tame the NT console).

    The only other option is PDcurses (old).

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    Sorry, I mispelled the name, I intended - as you said - pdcurses
    (pdcurses.sourceforge.net).
    Last was released in 2002.

    Best regards, bilbo

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by keats
    in this part by the help of gotoxy(), I'm first looking at the current location of x and y and then moving one space back on x-axis ... but as you said to expand it, I did it in the program quoted below ...
    A few notes:

    The coordinate (1,1) is the upper left corner, (XMAX,YMAX) is the lower right corner.

    You put stuff in the first row and the YMAX row, so your active edit area is from (1,2) to (XMAX,YMAX-1). You must always test cursor movements to prevent from going out of this area (Eventually, you will implement scrolling, I guess, when the cursor would go out of the range)

    You implement backspace by sending the following to the screen:

    Backspace, space, backspace

    So the code could look something like

    Code:
        if(key == 0x08) {
                x=wherex();
                y=wherey();
               /* put in a test here to see whether the cursor is already all of the way to the left*/
               /* if the cursor was all of the way to the left, decide what else to do */
               /* if not then do the following */
          
                gotoxy(--x,y);
                cprintf("%c", ' ');
                gotoxy(x,y);
              /* now do something to get back to the beginning of the loop*/
        }
    This erases the previous character and leaves the cursor sitting in the place where the erased character was located (in contrast to left arrow, which just moves the cursor to the left).

    Don't forget to test limits whenever the cursor is moved.

    I think that moving the cursor to the beginning of the next line is implemented by sending '\r' and '\n' to the screen rather than just '\n'.

    With my setup, getch() doesnot detect alt- keys (alt-s gives the same code as s, for example). You might consider using Fn keys (F1, F2, ...). Other implementations may differ, so your program might be OK for learning things about programming and learning things about what goes into a "notepad" type of program, but won't necessarily be practical for use by other people on other systems (learning about system dependencies is also valuable).

    These observations were based on using bcc32 (the free download) on Windows XP.

    I agree with others that stuff based on gotoxy(), wherex(), and other compiler-specific implementations may not be the best way to do things like this, but you can learn something with this project.



    Regards,

    Dave
    Last edited by Dave Evans; 06-17-2005 at 08:26 AM.

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    11
    The backspace problem is solved with the following code ...

    Code:
    	
    
    	   if(key == 0x0a || key == 0x0d)
    		   if(x != MAXX && y !=MAXY)
    			   key = '\n';
    	   cprintf("%c",key);
    	   if(key == 0x08) {
    
    			x=wherex();
    			y=wherey();
    			gotoxy(x,y);
    			cprintf("%c", ' ');
    			gotoxy(x,y);
    
    		             }
    
    	  x=wherex();
    	  y=wherey();
    	 }

    but gotoxy() problem is still alive ... please help ....... anyone ...

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by keats
    The backspace problem is solved with the following code ...

    but gotoxy() problem is still alive ... please help ....... anyone ...
    1. The cursor is sitting at position (x, y). If you want the backspace to go back to the previous position and erase that character and leave the cursor there, then you can use my code. Your code erases the character at the current position (not the usual behavior for a backspace in a text editor). Of course, when you really get into editing the text, you probably would want to move everything from the current position to the end of the line to the left (after deleting the character).

    2. If you want the cursor to go to the first column on the second row from the top, then gotoxy(1, 2), not (0, 1). I tried to give you this hint previously: (1, 1) is upper left, (MAXX, MAXY) is lower right.

    These work for me with the latest bcc32 (free download from Borland) on Windows XP.

    Regards,

    Dave
    Last edited by Dave Evans; 06-19-2005 at 08:45 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 05:39 PM
  3. text files & notepad problem
    By bigtamscot in forum C Programming
    Replies: 2
    Last Post: 05-01-2003, 04:41 PM
  4. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 11:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21