Maintain non-persistent database for books












6












$begingroup$


The following program displays a menu, and prompts the user to choose an option. Options include to add a book, delete a book, view all books, and to exit the program. (Note that the actual code to implement the database functions will be added later.)



The program files were compiled with no error messages or warnings using the following command line...



$gcc -std=c99 -Wall -Wextra -Wpedantic main.c screen.c input.c database.c -o books



I would like your feedback on any potential issues, along with any improvements that can be made to the code.



Here are the files:



main.c



#include <stdio.h>
#include <stdlib.h>
#include "struct.h"
#include "screen.h"
#include "input.h"
#include "database.h"

/* display main menu and prompt user to choose menu option */
int main(int argc, char *argv)
{
if (argc != 1)
{
fprintf(stderr, "Usage: %sn", argv[0]);
exit(EXIT_FAILURE);
}

/* name of file to store data in */
const char dataFile = "database.bin";

/* array of structs containing menu options (number, text, and function) */
menuStruct menuArray = {
{ 1, "Add book", addBook },
{ 2, "Delete book", deleteBook },
{ 3, "View all books", viewAllBooks }
};

/* number of menu items */
int menuArrayLength = sizeof(menuArray)/sizeof(menuArray[0]);

int menuChoice;
void(*menuFunction)(const char fname);

/* prompt user to choose menu option */
while (1)
{
clearScreen();
displayMainMenu(menuArray, menuArrayLength);
menuChoice = getMenuChoice();
if (menuChoice == (menuArrayLength + 1))
{
clearScreen();
exit(EXIT_SUCCESS);
}
menuFunction = getMenuFunction(menuChoice, menuArray, menuArrayLength);
if (menuFunction != NULL)
{
menuFunction(dataFile);
}
else
{
fprintf(stderr, "n Invalid entry, press ENTER to continue");
flushInput();
}
}

return 0;
}


struct.h



#ifndef STRUCT_H
#define STRUCT_H

#define MAXSTRLEN 50 /* max length for string */

/* struct to hold a menu choice (number, text, and function) */
typedef struct menuStruct
{
int menuChoice;
char menuText[MAXSTRLEN];
void (*menuFunction)(const char *fname);
} menuStruct;

/* struct to hold details of a book */
typedef struct bookStruct
{
char id[MAXSTRLEN];
char title[MAXSTRLEN];
char author[MAXSTRLEN];
double price;
} bookStruct;

#endif


screen.h



#ifndef SCREEN_H
#define SCREEN_H

#include "struct.h"

/* displayMainMenu(): display the main menu using menuArray where each element in the array
is a struct containing a menu choice (number, text, and function), except the exit option */
void displayMainMenu(const menuStruct menuArray, const int menuArrayLength);

/* displayAddBookHeader(): display the header for adding a book */
void displayAddBookHeader();

/* displayDeleteBookHeader(): display the header for deleting a book */
void displayDeleteBookHeader();

/* displayViewAllBooksHeader(): display the header for viewing all books */
void displayViewAllBooksHeader();

/* clearScreen(): clear the screen */
void clearScreen();

#endif


screen.c



#include <stdio.h>
#include "screen.h"

void displayMainMenu(const menuStruct menuArray, const int menuArrayLength)
{
printf(" n"
"=============================================================================n"
" Database for Book Collection n"
" Menu Choices n"
"=============================================================================n");

for (int i = 0; i < menuArrayLength; i++)
{
printf("n %d. %s", menuArray[i].menuChoice, menuArray[i].menuText);
}

printf("n %d. Exitn", menuArrayLength + 1); /* exit option for main menu */
}

void displayAddBookHeader()
{
printf(" n"
"=============================================================================n"
" Database for Book Collection n"
" Add Book n"
"=============================================================================n");
}


void displayDeleteBookHeader()
{
printf(" n"
"=============================================================================n"
" Database for Book Collection n"
" Delete Book n"
"=============================================================================n");
}

void displayViewAllBooksHeader()
{
printf(" n"
"=============================================================================n"
" Database for Book Collection n"
" View All Books n"
"=============================================================================n");

}

void clearScreen()
{
printf("x1b[2Jx1b[1;1H");
}


input.h



#ifndef INPUT_H
#define INPUT_H

#include "struct.h"

/* getMenuChoice(): prompt user to enter menu choice and return choice */
int getMenuChoice();

/* getMenuFunction(): search each struct within menuArray for option matching menuChoice and return corresponding function */
void(*getMenuFunction(const int menuChoice, const menuStruct menuArray, const int size))(const char fname);

/* flush input from buffer */
void flushInput();

#endif


input.c



#include <stdio.h>
#include <string.h>
#include "input.h"

#define MAXSTRLEN 50

int getMenuChoice()
{
printf("n Enter Choice: ");

char userInput[MAXSTRLEN], junk[MAXSTRLEN];
int menuChoice;


fgets(userInput, sizeof(userInput), stdin);
if (strchr(userInput, 'n') == NULL)
{
flushInput();
return 0;
}
if (sscanf(userInput, "%d%[^n]", &menuChoice, junk) != 1)
{
return 0;
}

return menuChoice;
}

void(*getMenuFunction(const int menuChoice, const menuStruct menuArray, const int size))(const char fname)
{
for (int i = 0; i < size; i++)
{
if (menuChoice == menuArray[i].menuChoice)
return menuArray[i].menuFunction;
}

return NULL;
}

void flushInput()
{
int c;
while ((c = getchar()) != 'n' && c != EOF)
{
/* skip it */;
}
}


database.h



#ifndef DATABASE_H
#define DATABASE_H

void addBook(const char fname);
void deleteBook(const char fname);
void viewAllBooks(const char fname);

#endif


database.c



#include <stdio.h>
#include <stdlib.h>
#include "input.h"
#include "database.h"

void addBook(const char fname)
{
printf("n Inside addBook(): fname = %s, press ENTER to continue", fname);
flushInput();
}

void deleteBook(const char fname)
{
printf("n Inside deleteBook(): fname = %s, press ENTER to continue", fname);
flushInput();
}

void viewAllBooks(const char fname)
{
printf("n Inside viewAllBooks(): fname = %s, press ENTER to continue", fname);
flushInput();
}









share|improve this question









New contributor




Domenic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    6












    $begingroup$


    The following program displays a menu, and prompts the user to choose an option. Options include to add a book, delete a book, view all books, and to exit the program. (Note that the actual code to implement the database functions will be added later.)



    The program files were compiled with no error messages or warnings using the following command line...



    $gcc -std=c99 -Wall -Wextra -Wpedantic main.c screen.c input.c database.c -o books



    I would like your feedback on any potential issues, along with any improvements that can be made to the code.



    Here are the files:



    main.c



    #include <stdio.h>
    #include <stdlib.h>
    #include "struct.h"
    #include "screen.h"
    #include "input.h"
    #include "database.h"

    /* display main menu and prompt user to choose menu option */
    int main(int argc, char *argv)
    {
    if (argc != 1)
    {
    fprintf(stderr, "Usage: %sn", argv[0]);
    exit(EXIT_FAILURE);
    }

    /* name of file to store data in */
    const char dataFile = "database.bin";

    /* array of structs containing menu options (number, text, and function) */
    menuStruct menuArray = {
    { 1, "Add book", addBook },
    { 2, "Delete book", deleteBook },
    { 3, "View all books", viewAllBooks }
    };

    /* number of menu items */
    int menuArrayLength = sizeof(menuArray)/sizeof(menuArray[0]);

    int menuChoice;
    void(*menuFunction)(const char fname);

    /* prompt user to choose menu option */
    while (1)
    {
    clearScreen();
    displayMainMenu(menuArray, menuArrayLength);
    menuChoice = getMenuChoice();
    if (menuChoice == (menuArrayLength + 1))
    {
    clearScreen();
    exit(EXIT_SUCCESS);
    }
    menuFunction = getMenuFunction(menuChoice, menuArray, menuArrayLength);
    if (menuFunction != NULL)
    {
    menuFunction(dataFile);
    }
    else
    {
    fprintf(stderr, "n Invalid entry, press ENTER to continue");
    flushInput();
    }
    }

    return 0;
    }


    struct.h



    #ifndef STRUCT_H
    #define STRUCT_H

    #define MAXSTRLEN 50 /* max length for string */

    /* struct to hold a menu choice (number, text, and function) */
    typedef struct menuStruct
    {
    int menuChoice;
    char menuText[MAXSTRLEN];
    void (*menuFunction)(const char *fname);
    } menuStruct;

    /* struct to hold details of a book */
    typedef struct bookStruct
    {
    char id[MAXSTRLEN];
    char title[MAXSTRLEN];
    char author[MAXSTRLEN];
    double price;
    } bookStruct;

    #endif


    screen.h



    #ifndef SCREEN_H
    #define SCREEN_H

    #include "struct.h"

    /* displayMainMenu(): display the main menu using menuArray where each element in the array
    is a struct containing a menu choice (number, text, and function), except the exit option */
    void displayMainMenu(const menuStruct menuArray, const int menuArrayLength);

    /* displayAddBookHeader(): display the header for adding a book */
    void displayAddBookHeader();

    /* displayDeleteBookHeader(): display the header for deleting a book */
    void displayDeleteBookHeader();

    /* displayViewAllBooksHeader(): display the header for viewing all books */
    void displayViewAllBooksHeader();

    /* clearScreen(): clear the screen */
    void clearScreen();

    #endif


    screen.c



    #include <stdio.h>
    #include "screen.h"

    void displayMainMenu(const menuStruct menuArray, const int menuArrayLength)
    {
    printf(" n"
    "=============================================================================n"
    " Database for Book Collection n"
    " Menu Choices n"
    "=============================================================================n");

    for (int i = 0; i < menuArrayLength; i++)
    {
    printf("n %d. %s", menuArray[i].menuChoice, menuArray[i].menuText);
    }

    printf("n %d. Exitn", menuArrayLength + 1); /* exit option for main menu */
    }

    void displayAddBookHeader()
    {
    printf(" n"
    "=============================================================================n"
    " Database for Book Collection n"
    " Add Book n"
    "=============================================================================n");
    }


    void displayDeleteBookHeader()
    {
    printf(" n"
    "=============================================================================n"
    " Database for Book Collection n"
    " Delete Book n"
    "=============================================================================n");
    }

    void displayViewAllBooksHeader()
    {
    printf(" n"
    "=============================================================================n"
    " Database for Book Collection n"
    " View All Books n"
    "=============================================================================n");

    }

    void clearScreen()
    {
    printf("x1b[2Jx1b[1;1H");
    }


    input.h



    #ifndef INPUT_H
    #define INPUT_H

    #include "struct.h"

    /* getMenuChoice(): prompt user to enter menu choice and return choice */
    int getMenuChoice();

    /* getMenuFunction(): search each struct within menuArray for option matching menuChoice and return corresponding function */
    void(*getMenuFunction(const int menuChoice, const menuStruct menuArray, const int size))(const char fname);

    /* flush input from buffer */
    void flushInput();

    #endif


    input.c



    #include <stdio.h>
    #include <string.h>
    #include "input.h"

    #define MAXSTRLEN 50

    int getMenuChoice()
    {
    printf("n Enter Choice: ");

    char userInput[MAXSTRLEN], junk[MAXSTRLEN];
    int menuChoice;


    fgets(userInput, sizeof(userInput), stdin);
    if (strchr(userInput, 'n') == NULL)
    {
    flushInput();
    return 0;
    }
    if (sscanf(userInput, "%d%[^n]", &menuChoice, junk) != 1)
    {
    return 0;
    }

    return menuChoice;
    }

    void(*getMenuFunction(const int menuChoice, const menuStruct menuArray, const int size))(const char fname)
    {
    for (int i = 0; i < size; i++)
    {
    if (menuChoice == menuArray[i].menuChoice)
    return menuArray[i].menuFunction;
    }

    return NULL;
    }

    void flushInput()
    {
    int c;
    while ((c = getchar()) != 'n' && c != EOF)
    {
    /* skip it */;
    }
    }


    database.h



    #ifndef DATABASE_H
    #define DATABASE_H

    void addBook(const char fname);
    void deleteBook(const char fname);
    void viewAllBooks(const char fname);

    #endif


    database.c



    #include <stdio.h>
    #include <stdlib.h>
    #include "input.h"
    #include "database.h"

    void addBook(const char fname)
    {
    printf("n Inside addBook(): fname = %s, press ENTER to continue", fname);
    flushInput();
    }

    void deleteBook(const char fname)
    {
    printf("n Inside deleteBook(): fname = %s, press ENTER to continue", fname);
    flushInput();
    }

    void viewAllBooks(const char fname)
    {
    printf("n Inside viewAllBooks(): fname = %s, press ENTER to continue", fname);
    flushInput();
    }









    share|improve this question









    New contributor




    Domenic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      6












      6








      6





      $begingroup$


      The following program displays a menu, and prompts the user to choose an option. Options include to add a book, delete a book, view all books, and to exit the program. (Note that the actual code to implement the database functions will be added later.)



      The program files were compiled with no error messages or warnings using the following command line...



      $gcc -std=c99 -Wall -Wextra -Wpedantic main.c screen.c input.c database.c -o books



      I would like your feedback on any potential issues, along with any improvements that can be made to the code.



      Here are the files:



      main.c



      #include <stdio.h>
      #include <stdlib.h>
      #include "struct.h"
      #include "screen.h"
      #include "input.h"
      #include "database.h"

      /* display main menu and prompt user to choose menu option */
      int main(int argc, char *argv)
      {
      if (argc != 1)
      {
      fprintf(stderr, "Usage: %sn", argv[0]);
      exit(EXIT_FAILURE);
      }

      /* name of file to store data in */
      const char dataFile = "database.bin";

      /* array of structs containing menu options (number, text, and function) */
      menuStruct menuArray = {
      { 1, "Add book", addBook },
      { 2, "Delete book", deleteBook },
      { 3, "View all books", viewAllBooks }
      };

      /* number of menu items */
      int menuArrayLength = sizeof(menuArray)/sizeof(menuArray[0]);

      int menuChoice;
      void(*menuFunction)(const char fname);

      /* prompt user to choose menu option */
      while (1)
      {
      clearScreen();
      displayMainMenu(menuArray, menuArrayLength);
      menuChoice = getMenuChoice();
      if (menuChoice == (menuArrayLength + 1))
      {
      clearScreen();
      exit(EXIT_SUCCESS);
      }
      menuFunction = getMenuFunction(menuChoice, menuArray, menuArrayLength);
      if (menuFunction != NULL)
      {
      menuFunction(dataFile);
      }
      else
      {
      fprintf(stderr, "n Invalid entry, press ENTER to continue");
      flushInput();
      }
      }

      return 0;
      }


      struct.h



      #ifndef STRUCT_H
      #define STRUCT_H

      #define MAXSTRLEN 50 /* max length for string */

      /* struct to hold a menu choice (number, text, and function) */
      typedef struct menuStruct
      {
      int menuChoice;
      char menuText[MAXSTRLEN];
      void (*menuFunction)(const char *fname);
      } menuStruct;

      /* struct to hold details of a book */
      typedef struct bookStruct
      {
      char id[MAXSTRLEN];
      char title[MAXSTRLEN];
      char author[MAXSTRLEN];
      double price;
      } bookStruct;

      #endif


      screen.h



      #ifndef SCREEN_H
      #define SCREEN_H

      #include "struct.h"

      /* displayMainMenu(): display the main menu using menuArray where each element in the array
      is a struct containing a menu choice (number, text, and function), except the exit option */
      void displayMainMenu(const menuStruct menuArray, const int menuArrayLength);

      /* displayAddBookHeader(): display the header for adding a book */
      void displayAddBookHeader();

      /* displayDeleteBookHeader(): display the header for deleting a book */
      void displayDeleteBookHeader();

      /* displayViewAllBooksHeader(): display the header for viewing all books */
      void displayViewAllBooksHeader();

      /* clearScreen(): clear the screen */
      void clearScreen();

      #endif


      screen.c



      #include <stdio.h>
      #include "screen.h"

      void displayMainMenu(const menuStruct menuArray, const int menuArrayLength)
      {
      printf(" n"
      "=============================================================================n"
      " Database for Book Collection n"
      " Menu Choices n"
      "=============================================================================n");

      for (int i = 0; i < menuArrayLength; i++)
      {
      printf("n %d. %s", menuArray[i].menuChoice, menuArray[i].menuText);
      }

      printf("n %d. Exitn", menuArrayLength + 1); /* exit option for main menu */
      }

      void displayAddBookHeader()
      {
      printf(" n"
      "=============================================================================n"
      " Database for Book Collection n"
      " Add Book n"
      "=============================================================================n");
      }


      void displayDeleteBookHeader()
      {
      printf(" n"
      "=============================================================================n"
      " Database for Book Collection n"
      " Delete Book n"
      "=============================================================================n");
      }

      void displayViewAllBooksHeader()
      {
      printf(" n"
      "=============================================================================n"
      " Database for Book Collection n"
      " View All Books n"
      "=============================================================================n");

      }

      void clearScreen()
      {
      printf("x1b[2Jx1b[1;1H");
      }


      input.h



      #ifndef INPUT_H
      #define INPUT_H

      #include "struct.h"

      /* getMenuChoice(): prompt user to enter menu choice and return choice */
      int getMenuChoice();

      /* getMenuFunction(): search each struct within menuArray for option matching menuChoice and return corresponding function */
      void(*getMenuFunction(const int menuChoice, const menuStruct menuArray, const int size))(const char fname);

      /* flush input from buffer */
      void flushInput();

      #endif


      input.c



      #include <stdio.h>
      #include <string.h>
      #include "input.h"

      #define MAXSTRLEN 50

      int getMenuChoice()
      {
      printf("n Enter Choice: ");

      char userInput[MAXSTRLEN], junk[MAXSTRLEN];
      int menuChoice;


      fgets(userInput, sizeof(userInput), stdin);
      if (strchr(userInput, 'n') == NULL)
      {
      flushInput();
      return 0;
      }
      if (sscanf(userInput, "%d%[^n]", &menuChoice, junk) != 1)
      {
      return 0;
      }

      return menuChoice;
      }

      void(*getMenuFunction(const int menuChoice, const menuStruct menuArray, const int size))(const char fname)
      {
      for (int i = 0; i < size; i++)
      {
      if (menuChoice == menuArray[i].menuChoice)
      return menuArray[i].menuFunction;
      }

      return NULL;
      }

      void flushInput()
      {
      int c;
      while ((c = getchar()) != 'n' && c != EOF)
      {
      /* skip it */;
      }
      }


      database.h



      #ifndef DATABASE_H
      #define DATABASE_H

      void addBook(const char fname);
      void deleteBook(const char fname);
      void viewAllBooks(const char fname);

      #endif


      database.c



      #include <stdio.h>
      #include <stdlib.h>
      #include "input.h"
      #include "database.h"

      void addBook(const char fname)
      {
      printf("n Inside addBook(): fname = %s, press ENTER to continue", fname);
      flushInput();
      }

      void deleteBook(const char fname)
      {
      printf("n Inside deleteBook(): fname = %s, press ENTER to continue", fname);
      flushInput();
      }

      void viewAllBooks(const char fname)
      {
      printf("n Inside viewAllBooks(): fname = %s, press ENTER to continue", fname);
      flushInput();
      }









      share|improve this question









      New contributor




      Domenic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      The following program displays a menu, and prompts the user to choose an option. Options include to add a book, delete a book, view all books, and to exit the program. (Note that the actual code to implement the database functions will be added later.)



      The program files were compiled with no error messages or warnings using the following command line...



      $gcc -std=c99 -Wall -Wextra -Wpedantic main.c screen.c input.c database.c -o books



      I would like your feedback on any potential issues, along with any improvements that can be made to the code.



      Here are the files:



      main.c



      #include <stdio.h>
      #include <stdlib.h>
      #include "struct.h"
      #include "screen.h"
      #include "input.h"
      #include "database.h"

      /* display main menu and prompt user to choose menu option */
      int main(int argc, char *argv)
      {
      if (argc != 1)
      {
      fprintf(stderr, "Usage: %sn", argv[0]);
      exit(EXIT_FAILURE);
      }

      /* name of file to store data in */
      const char dataFile = "database.bin";

      /* array of structs containing menu options (number, text, and function) */
      menuStruct menuArray = {
      { 1, "Add book", addBook },
      { 2, "Delete book", deleteBook },
      { 3, "View all books", viewAllBooks }
      };

      /* number of menu items */
      int menuArrayLength = sizeof(menuArray)/sizeof(menuArray[0]);

      int menuChoice;
      void(*menuFunction)(const char fname);

      /* prompt user to choose menu option */
      while (1)
      {
      clearScreen();
      displayMainMenu(menuArray, menuArrayLength);
      menuChoice = getMenuChoice();
      if (menuChoice == (menuArrayLength + 1))
      {
      clearScreen();
      exit(EXIT_SUCCESS);
      }
      menuFunction = getMenuFunction(menuChoice, menuArray, menuArrayLength);
      if (menuFunction != NULL)
      {
      menuFunction(dataFile);
      }
      else
      {
      fprintf(stderr, "n Invalid entry, press ENTER to continue");
      flushInput();
      }
      }

      return 0;
      }


      struct.h



      #ifndef STRUCT_H
      #define STRUCT_H

      #define MAXSTRLEN 50 /* max length for string */

      /* struct to hold a menu choice (number, text, and function) */
      typedef struct menuStruct
      {
      int menuChoice;
      char menuText[MAXSTRLEN];
      void (*menuFunction)(const char *fname);
      } menuStruct;

      /* struct to hold details of a book */
      typedef struct bookStruct
      {
      char id[MAXSTRLEN];
      char title[MAXSTRLEN];
      char author[MAXSTRLEN];
      double price;
      } bookStruct;

      #endif


      screen.h



      #ifndef SCREEN_H
      #define SCREEN_H

      #include "struct.h"

      /* displayMainMenu(): display the main menu using menuArray where each element in the array
      is a struct containing a menu choice (number, text, and function), except the exit option */
      void displayMainMenu(const menuStruct menuArray, const int menuArrayLength);

      /* displayAddBookHeader(): display the header for adding a book */
      void displayAddBookHeader();

      /* displayDeleteBookHeader(): display the header for deleting a book */
      void displayDeleteBookHeader();

      /* displayViewAllBooksHeader(): display the header for viewing all books */
      void displayViewAllBooksHeader();

      /* clearScreen(): clear the screen */
      void clearScreen();

      #endif


      screen.c



      #include <stdio.h>
      #include "screen.h"

      void displayMainMenu(const menuStruct menuArray, const int menuArrayLength)
      {
      printf(" n"
      "=============================================================================n"
      " Database for Book Collection n"
      " Menu Choices n"
      "=============================================================================n");

      for (int i = 0; i < menuArrayLength; i++)
      {
      printf("n %d. %s", menuArray[i].menuChoice, menuArray[i].menuText);
      }

      printf("n %d. Exitn", menuArrayLength + 1); /* exit option for main menu */
      }

      void displayAddBookHeader()
      {
      printf(" n"
      "=============================================================================n"
      " Database for Book Collection n"
      " Add Book n"
      "=============================================================================n");
      }


      void displayDeleteBookHeader()
      {
      printf(" n"
      "=============================================================================n"
      " Database for Book Collection n"
      " Delete Book n"
      "=============================================================================n");
      }

      void displayViewAllBooksHeader()
      {
      printf(" n"
      "=============================================================================n"
      " Database for Book Collection n"
      " View All Books n"
      "=============================================================================n");

      }

      void clearScreen()
      {
      printf("x1b[2Jx1b[1;1H");
      }


      input.h



      #ifndef INPUT_H
      #define INPUT_H

      #include "struct.h"

      /* getMenuChoice(): prompt user to enter menu choice and return choice */
      int getMenuChoice();

      /* getMenuFunction(): search each struct within menuArray for option matching menuChoice and return corresponding function */
      void(*getMenuFunction(const int menuChoice, const menuStruct menuArray, const int size))(const char fname);

      /* flush input from buffer */
      void flushInput();

      #endif


      input.c



      #include <stdio.h>
      #include <string.h>
      #include "input.h"

      #define MAXSTRLEN 50

      int getMenuChoice()
      {
      printf("n Enter Choice: ");

      char userInput[MAXSTRLEN], junk[MAXSTRLEN];
      int menuChoice;


      fgets(userInput, sizeof(userInput), stdin);
      if (strchr(userInput, 'n') == NULL)
      {
      flushInput();
      return 0;
      }
      if (sscanf(userInput, "%d%[^n]", &menuChoice, junk) != 1)
      {
      return 0;
      }

      return menuChoice;
      }

      void(*getMenuFunction(const int menuChoice, const menuStruct menuArray, const int size))(const char fname)
      {
      for (int i = 0; i < size; i++)
      {
      if (menuChoice == menuArray[i].menuChoice)
      return menuArray[i].menuFunction;
      }

      return NULL;
      }

      void flushInput()
      {
      int c;
      while ((c = getchar()) != 'n' && c != EOF)
      {
      /* skip it */;
      }
      }


      database.h



      #ifndef DATABASE_H
      #define DATABASE_H

      void addBook(const char fname);
      void deleteBook(const char fname);
      void viewAllBooks(const char fname);

      #endif


      database.c



      #include <stdio.h>
      #include <stdlib.h>
      #include "input.h"
      #include "database.h"

      void addBook(const char fname)
      {
      printf("n Inside addBook(): fname = %s, press ENTER to continue", fname);
      flushInput();
      }

      void deleteBook(const char fname)
      {
      printf("n Inside deleteBook(): fname = %s, press ENTER to continue", fname);
      flushInput();
      }

      void viewAllBooks(const char fname)
      {
      printf("n Inside viewAllBooks(): fname = %s, press ENTER to continue", fname);
      flushInput();
      }






      c database






      share|improve this question









      New contributor




      Domenic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Domenic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 6 hours ago









      200_success

      129k15153415




      129k15153415






      New contributor




      Domenic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 10 hours ago









      DomenicDomenic

      1312




      1312




      New contributor




      Domenic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Domenic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Domenic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          1












          $begingroup$

          Be wary of this:




          void clearScreen()
          {
          printf("x1b[2Jx1b[1;1H");
          }



          Whilst many terminals support the ANSI command set, not all do, so hard-coding this escape code will limit the program's flexibility.



          There are libraries (such as Curses) that help with this, but that's likely overkill for this purpose. The pragmatic approach here is to outsource to the standard command using system() - on POSIX systems, you'll just invoke clear, for example.



          That said, I'd advise against clearing screen repeatedly - it makes it much harder for the user to go back and review the actions that have been performed.






          share|improve this answer









          $endgroup$













          • $begingroup$
            I thought clearing the screen would present things in a more "clean" fashion, but I agree with your point. I'll avoid it completely. Toby, thank you very much for your help. Cheers!
            $endgroup$
            – Domenic
            6 hours ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Domenic is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213390%2fmaintain-non-persistent-database-for-books%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1












          $begingroup$

          Be wary of this:




          void clearScreen()
          {
          printf("x1b[2Jx1b[1;1H");
          }



          Whilst many terminals support the ANSI command set, not all do, so hard-coding this escape code will limit the program's flexibility.



          There are libraries (such as Curses) that help with this, but that's likely overkill for this purpose. The pragmatic approach here is to outsource to the standard command using system() - on POSIX systems, you'll just invoke clear, for example.



          That said, I'd advise against clearing screen repeatedly - it makes it much harder for the user to go back and review the actions that have been performed.






          share|improve this answer









          $endgroup$













          • $begingroup$
            I thought clearing the screen would present things in a more "clean" fashion, but I agree with your point. I'll avoid it completely. Toby, thank you very much for your help. Cheers!
            $endgroup$
            – Domenic
            6 hours ago
















          1












          $begingroup$

          Be wary of this:




          void clearScreen()
          {
          printf("x1b[2Jx1b[1;1H");
          }



          Whilst many terminals support the ANSI command set, not all do, so hard-coding this escape code will limit the program's flexibility.



          There are libraries (such as Curses) that help with this, but that's likely overkill for this purpose. The pragmatic approach here is to outsource to the standard command using system() - on POSIX systems, you'll just invoke clear, for example.



          That said, I'd advise against clearing screen repeatedly - it makes it much harder for the user to go back and review the actions that have been performed.






          share|improve this answer









          $endgroup$













          • $begingroup$
            I thought clearing the screen would present things in a more "clean" fashion, but I agree with your point. I'll avoid it completely. Toby, thank you very much for your help. Cheers!
            $endgroup$
            – Domenic
            6 hours ago














          1












          1








          1





          $begingroup$

          Be wary of this:




          void clearScreen()
          {
          printf("x1b[2Jx1b[1;1H");
          }



          Whilst many terminals support the ANSI command set, not all do, so hard-coding this escape code will limit the program's flexibility.



          There are libraries (such as Curses) that help with this, but that's likely overkill for this purpose. The pragmatic approach here is to outsource to the standard command using system() - on POSIX systems, you'll just invoke clear, for example.



          That said, I'd advise against clearing screen repeatedly - it makes it much harder for the user to go back and review the actions that have been performed.






          share|improve this answer









          $endgroup$



          Be wary of this:




          void clearScreen()
          {
          printf("x1b[2Jx1b[1;1H");
          }



          Whilst many terminals support the ANSI command set, not all do, so hard-coding this escape code will limit the program's flexibility.



          There are libraries (such as Curses) that help with this, but that's likely overkill for this purpose. The pragmatic approach here is to outsource to the standard command using system() - on POSIX systems, you'll just invoke clear, for example.



          That said, I'd advise against clearing screen repeatedly - it makes it much harder for the user to go back and review the actions that have been performed.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 9 hours ago









          Toby SpeightToby Speight

          24.4k740114




          24.4k740114












          • $begingroup$
            I thought clearing the screen would present things in a more "clean" fashion, but I agree with your point. I'll avoid it completely. Toby, thank you very much for your help. Cheers!
            $endgroup$
            – Domenic
            6 hours ago


















          • $begingroup$
            I thought clearing the screen would present things in a more "clean" fashion, but I agree with your point. I'll avoid it completely. Toby, thank you very much for your help. Cheers!
            $endgroup$
            – Domenic
            6 hours ago
















          $begingroup$
          I thought clearing the screen would present things in a more "clean" fashion, but I agree with your point. I'll avoid it completely. Toby, thank you very much for your help. Cheers!
          $endgroup$
          – Domenic
          6 hours ago




          $begingroup$
          I thought clearing the screen would present things in a more "clean" fashion, but I agree with your point. I'll avoid it completely. Toby, thank you very much for your help. Cheers!
          $endgroup$
          – Domenic
          6 hours ago










          Domenic is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Domenic is a new contributor. Be nice, and check out our Code of Conduct.













          Domenic is a new contributor. Be nice, and check out our Code of Conduct.












          Domenic is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213390%2fmaintain-non-persistent-database-for-books%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to make a Squid Proxy server?

          第一次世界大戦

          Touch on Surface Book