Saturday 1 June 2013

The Database? Not really.

I first wanted to program when I saw the text based applications (Wordstar, Lotus 123, DBase) in the early 90's. That is why I bought the C books back then, but I was in my early teens, and I'm kind of average intelligence, so never really understood all the stuff in the books.

Oh yeah , and I didn't have a computer, only my friend did! Heh!. It had no hard-drive only a 5 1/2 inch floppy drive. We had to boot it up with a DOS disk, then remove the disk and slip in the disk of the application we wanted to use. Ah good times, good times! Lol.

But now that DOS based applications (and DOS based programming) are being phased out, I will have to learn Windows programming too.
I have a page bookmarked for later, to begin learning once I gain a mediocre understanding of C.
http://www.winprog.org/tutorial/start.html

Anyway, another messy program that I had to put a lot of effort in, coming up. It started with a program in Chapter 7 of Let Us C (Macros With Arguments) Ex-c. The program drew a horizontal and vertical line using multiple line macros.

I started changing things around, got rid of the macros and got myself to draw a double line "plus" using ASCII characters.


Then I wondered if I could get the program to accept input in only one part of the 4 'squares' that are drawn, wondering in the back of my head if this is what the app I’ll write for my friend’s shop will begin from....heh!

Again, this mess of a program will look better when you copy and paste into Code Blocks. I'm just trying to use and reuse the little I know so that they become firmly embedded in my mind.
It took me a while to limit the users input and move the cursor to the next line when they had entered 39 characters.
It may be a mess & obviously incomplete, but it represents a whole night of work for me.

This is what I ended up with eventually:

#include <myconiowin.h> //see previous post to include this file.
#include <stdio.h>

void tab(void);
void entr(void);
void backspace(void);

int enter=3,w=0,a=0,b,z=0;
    /*  'enter' for number of lines,'w' to track the number of characters in the previous line,
        'a=0' for tab key tracking, 'b' for backspace loop, 'z' to store # of characters
        typed. Enter starts at 3 because we will display the cursor starting at the 3rd line
    */

main()
{
    system("cls");
    /*This part of the code draws the lines*/
    int x;
    SetColorAndBackground(12,0);

    for(x=0;x<25;x++)
    {
        gotoxy(39,x);
        printf("%c",186); //186,
    }


    for(x=0;x<39;x++)
    {
        gotoxy(x,11);
        printf("%c",205); //196
    }

    printf("%c",206);   //206,197

    for(x=40;x<80;x++)
    {
        gotoxy(x,11);
        printf("%c",205); //205,196
    }

    gotoxy(0,0); //home the cursor.
    SetColorAndBackground(10,0); //Green text, black background.
    printf("Press ~ to exit."); //print Exit Message.
    gotoxy(40,3);                  //Move cursor to second Box.
    SetColorAndBackground(15,0);    //White text against black background.
    char c1;                        //To hold the keys typed.



    for(;;)                         //Infinite Loop
    {
        c1=getche();                //Character typed is stored in c1 and displayed.
        if(c1=='~')             //If character is ~ exit.
        {
            system("cls");
            exit(0);
        }


        else if(c1>0 && c1<8 || c1>9 && c1<13 || c1>13 && c1<255) //if valid ascii characters
        {
            z++; //z(number of characters) is incremented by 1
            w++; //w(number of characters in one line) in incremented.

            if(w==39 && enter!=10) //39 characters per line
            {
                enter++;    //number of newlines is incremented by 1 and....
                gotoxy(40,enter); //cursor is sent to the next line
                w=0; //number of characters in the newline is reset to zero.
            }

            else if(w==39 && enter==10)
            {
                gotoxy(79,10);
                printf(" \b \b\b");    //erase one character
                gotoxy(40,0);
                printf("Out of space!");
                gotoxy(79,10); //so that cursor looks like it's in position
                getch();    //wait for keypress
                gotoxy(53,0);               //Cursor moves to end of the displayed text 40+13=53
                                            //13 is the number of characters in the message
                for(b=0;b<13;b++)           //This loop erases the 13 characters in the message
                {
                    printf("\b \b");
                }
                w--;
                z--;
                gotoxy(79,10);   //go to last character of last line. (40+39=79)
            }

        }

        else if(c1==9) //The tab key
        {
            tab();
        }

        else if(c1==8) //backspace is pressed
        {
           backspace();
        }

        else if(c1==13) //enter key is pressed
        {
           entr();
        }

    }

}

void tab(void)
{
    if(enter==10) //number of enters has reached the bottom of the box
            {
                gotoxy(40,0);
                printf("Out of space!");
                gotoxy(40+w,10); //so that cursor look like its in place
                getch();    //wait fpr keypress
                gotoxy(53,0);               //Cursor moves to end of the displayed text 40+13=53
                                            //13 is the number of characters in the message
                for(b=0;b<13;b++)           //This loop erases the 13 characters in the message
                {
                    printf("\b \b");
                }
                gotoxy(40+w,10);   //go to first character of last line + number of ch.
            }
            a++;         //We track the tab key
            z+=8;    //z(number of characters) is incremented by 8(more or less space taken by tab)
            w+=8;    //w(number of characters in one line) in incremented.
            if(a==5)    //user may only put in 4 tabs before the cursor moves to the beginning
                        //of next line
            {
                enter++;    //number of newlines is incremented by 1 and....
                gotoxy(40,enter); //cursor is sent to the next line
                a=0;              //since we are at a new line, a(tabs) is reset to zero.
                w=0;               //number of characters in a line is reset to 0
            }
}

void entr(void)
{
     if(enter==10 && w>=0) //number of enters has reached the bottom of the box
            {
                gotoxy(40,0);
                printf("Out of space!");
                gotoxy(40+w,10); //so that cursor look like its in place
                getch();    //wait fpr keypress
                gotoxy(53,0);               //Cursor moves to end of the displayed text 40+13=53
                                            //34 is the number of characters in the message
                for(b=0;b<13;b++)           //This loop erases the 34 characters in the message
                {
                    printf("\b \b");
                }
                gotoxy(40+w,10);   //go to first character of last line + number of ch.
            }

            else
            {
            enter++;    //if the above doesnt happen, IE enter is less than 10, enter incremented
            gotoxy(40,enter); //cursor moves to beginning of next line.
            w=39-w; //max characters minus characters entered gives the remainder to reach 39
            z=z+w;  //it is added to the total number of characters
            w=0;    //w (characters typed in a line) is reset to 0 to track new line
            /*The small limited line can only hold 4 tab keys. If the user had pressed tab a few times
            it is also reset to 0 for the new line.*/
            a==0;
            }
}

void backspace(void)
{
     if(z>0 && w>0)
        /*If there are characters*/
            {
                printf(" \b");  //This moves the cursor back one line and prints a white space.
                                //effectively deleting the character before the cursor.
                    z--; //Number of characters is decremented.
                    w--; //number of character line wise is decremented
            }

            /*If the cursor is at the beginning of line 4,5,6,7,8 or 9, the cursor moves to the
            end of the previous line*/
            else if(z>0 && enter>3 && enter!=10 && w==0)
            {
                gotoxy(40+39,enter-1); //40(beginning of line) plus 39 characters which the user has typed
                                        //and enter(the current line) minus one.

                enter--;                //enter is decremented.
                w=39;
                a=0;                     //tabs is reset to 0

            }

            else if(z>0 && enter>3 && enter==10 && w==0)
            {
                printf(" "); //this line was what fixed the above problem
                gotoxy(40+39,enter-1); //40(beginning of line) plus 39 characters which the user has typed
                                        //and enter(the current line) minus one.

                enter--;                //enter is decremented.
                w=39;
                a=0;                     //tabs is reset to 0
            }


            /*if the cursor is at the beginning of the first line, and the number of characters is zero
            it will inform the user that no text exists to backspace*/
            else if(enter==3 && z==0 && w==0)
            {
                SetColorAndBackground(12,0); //text red
                printf("%c",186);          //since the backspace will happen, it reprints the ASCII code
                                        //To prevent the user from seeing the line broken by the backspace.
                gotoxy(40,0);              //cursor moves to top of second box.
                printf("Cannot backspace: no text."); //This text is displayed.
                gotoxy(40,enter);   //so that cursor looks like it's still in position
                getch();                    //Waits for keypress.
                gotoxy(66,0);               //Cursor moves to end of the displayed text 40+26=66
                                            //34 is the number of characters in the message
                for(b=0;b<26;b++)           //This loop erases the 26 characters in the message
                {
                    printf("\b \b");
                }
                SetColorAndBackground(15,0); //Colour is set back to white
                gotoxy(40,enter);               //cursor is moved to where it should be.
            }
            else if(enter==3 && z>0)
            {
                gotoxy(40,enter);
                z--;
                w==0;
            }

            else if(enter>3 && w==0 && z==0)
            {
                gotoxy(40,enter-1); //40(beginning of line) and enter(the current line) minus one.

                enter--;                //enter is decremented.
                w=0;
                a=0;                     //tabs is reset to 0
            }
}

No comments:

Post a Comment