Sunday, July 5, 2020

complete menu driven programm in centos7.5 and codeblocks

complete menu driven program :-

file main.cpp :-
------------------

    #include "draw.h"
    #include "getchoice.h"


    using std::cin;
    using std::cout;

    char * menu[] ={
        "1.  Add New Product.",
        "2.  Add New Members.",
        "3.  View An Existing Product Records.",
        "4.  View An Existing Member's Record.",
        "5.  Billing.",
        "6.  Today's Sail.",
        "7.  Modify Product Record.",
        "8.  Modify Member's Record.",
        "9.  Instructions.",
        "10. Exit.",
        NULL
    };


    int main()
    {

        int choice = 0;
        clrscr();
        drawrect();
        FILE *input, *output;

        if (!isatty(fileno(stdout)))
        {
            fprintf(stderr,"You are not a terminal, OK.\n");
        }

        input = fopen("/dev/tty", "r");
        output = fopen("/dev/tty", "w");
        if(!input || !output)
        {
            fprintf(stderr,"Unable to open /dev/tty\n");
            exit(1);
        }

        do
        {
            choice = getchoice(menu, input, output);
            switch(choice)
            {
            case 1:
               func1(); break;
            case 2:
                func2(); break;
            case 3:
                func3(); break;
            case 4:
                func4(); break;
            case 5:
                func5();  break;
            case 6:
                func6(); break;
            case 7:
                func7(); break;
            case 8:
                func8(); break;
            case 9:
                func9(); break;
            case 10:
                clrscr();
                exit(0);
            }

        } while (choice != 0);
        return 0;
    }


file draw.h :-
----------------

    #ifndef DRAW_H_INCLUDED
    #define DRAW_H_INCLUDED

    #include <iostream>
    #include <cstdio>
    #include <sys/ioctl.h>
    #include <unistd.h>
    #include "draw.h"

    void gotoxy(int x,int y);
    void clrscr();
    void drawrect();

    #endif // DRAW_H_INCLUDED


file draw.cpp :-
------------------

    #include "draw.h"

    using std::cout;

    void gotoxy(int x,int y)
    {
        printf("%c[%d;%df",0x1B,y,x);
    }

    void clrscr()
    {
        gotoxy(0,0);

        struct winsize w;
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

        for (int x = 0; x < w.ws_row; x++)
        {
            for(int y = 0; y < w.ws_col; y++)
            {
                cout << " ";
            }
        }
        gotoxy(0, 0);
    }
    void drawrect()
    {
        winsize w;
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

        for(int i = 0; i <= w.ws_col; i++)
        {
            gotoxy(i, 2);
            cout << "-";

            gotoxy(i, w.ws_row);
            cout  << "-";
        }
        for ( int i = 2; i <= w.ws_row; i++)
        {
            gotoxy(1, i);
            cout << "|";
            gotoxy(w.ws_col, i);
            cout << "|";
        }
        gotoxy(0,0);
    }


file getchoice.h :-
---------------------

    #ifndef GETCHOICE_H_INCLUDED
    #define GETCHOICE_H_INCLUDED

    #include "draw.h"
    #include <cstdlib>
    #include <cstring>

    int getchoice(char *choices[], FILE *in, FILE *out);


    #endif // GETCHOICE_H_INCLUDED


file getchoice.cpp :-
------------------------

    #include "getchoice.h"

    using std::cout;
    using std::cin;

    int getchoice(char *choices[], FILE *in, FILE *out)
    {
        int chosen = 0;
        char **option, *ch, selection[3], choicechar;
        int i, j, k, l, m, len;

        char *p, *wp;

        ch = (char*)malloc(5);

    LABEL1: do {
            drawrect();

            gotoxy(25, 3);
            cout <<"MENU";
            gotoxy(23, 4);

            for( i = 23; i < 31; i++)
                cout << "-";

            l = 5;
            gotoxy(15, l);
            option = choices;
            l++;
            while(*option)
            {
                gotoxy(15, l);
                cout << *option;
                option++;
                l++;
            }

            gotoxy(15, ++l);
            cout << "Enter choice : ";

                ch = fgets(ch, 5, in);
                ch[strcspn(ch, "\r\n")] = '\0';
                m = atoi(ch);

            option = choices;

            j = 0;
            while(*option)
            {
                k = 0;
                while((choicechar = option[j][k]) != '.')
                {
                    selection[k] = choicechar;
                    k++;
                }
                selection[k] = '\0';
                if(m == atoi(selection))
                {
                    chosen = 1;
                    strcpy(ch, "");
                    break;
                }

                j++;

                if(option[j] == NULL)
                {
                    gotoxy(15, ++l);
                    cout << "Incorrect choice, select again";
                    cin.get();
                    clrscr();
                    goto LABEL1;
                    break;
                }
            }
        } while(!chosen);
      
        return m;
    }

output :-

No comments:

Post a Comment