Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am in an introductory C++ class, and I am not sure how to complete the last ha

ID: 3842826 • Letter: I

Question

I am in an introductory C++ class, and I am not sure how to complete the last half of the program. Help? I need to do the addEntry, deleteEntry, searchEntry,readFromFile, and SavetoFile and cannot seem to get it right at all. Thank you.

/********************************************************************
PROJECT: Glossary

FILE: glossary.c

DESCRIPTION: A program that reads, writes, and maintains a simple glossary
of terms and definitions.

REQUIREMENTS:
R01 User is prompted for action (code provided), or given the option
to exit the program.

R02 The first term containing the search string is the entry that is
displayed to the user.

R03 Terms are to be maintained in alphabetical order.

R04 If the user selects [R]ead from file, existing glossary entries
shall not be overwritten.

R05 If the user selects [S]ave to file, and the file exists, the user
shall be warned before overwriting the file.

DESIGN
Significant Variables
---------------------
struct item {
char term[25];
char defn[255];
};
struct item glossary[MAX_ITEMS];

Algorithm
---------
A00 Prompt user for selection;
A05 If (valid entry) {
Call corresponding function call;
}
A10 Else {
Prompt again;
}
A15 Prompt user to save work
A20
Return 0;
********************************************************************/

#include
#include
#include

/* Definitions */
const int MAX_TERM_LEN = 25;
const int MAX_DEFN_LEN = 255;
const int MAX_ITEMS = 500;

struct item {
char term[25];
char defn[255];
};

/* Function prototypes */
int addEntry(struct item[]);
int deleteEntry(struct item[]);
int searchEntry(struct item[]);
int readFromFile(struct item[]);
int saveToFile(struct item[]);

int main() {
/* Declarations */
char action;

int numItems, result;

struct item glossary[MAX_ITEMS];

/*L00*/
/* Prompt loop */
printf("[A]dd, [D]elete, Searc[H], [R]ead from file, [S]ave, [E]nd>");
scanf("%c", &action);
fgetc(stdin);
/*L05*/
while(action != 'E') {
if(action == 'A') {
result = addEntry(glossary);
if(result < 0) {
printf("Entry exists! ");
}
else {
printf("Add entry successful. ");
}
}
else if(action == 'D') {
result = deleteEntry(glossary);
if(result < 0) {
printf("Entry does not exist! ");
}
else {
printf("Delete entry successful. ");
}
}
else if(action == 'H') {
result = searchEntry(glossary);
if(result < 0) {
printf("Entry not found! ");
}
}
else if(action == 'R') {
result = readFromFile(glossary);
if(result < 0) {
printf("Error reading from file! ");
}
else {
printf("Read successful. ");
}
}
else if(action == 'S') {
result = saveToFile(glossary);
if(result < 0) {
printf("Error saving to file! ");
}
else {
printf("Save successful. ");
}
}
/*L10*/
printf("[A]dd, [D]elete, Searc[H], [R]ead from file, [S]ave, [E]nd> ");
scanf("%c", &action);
fgetc(stdin);
}
/*L15*/
printf("Do you want to save your work before exiting? (Y or N)> ");
scanf("%c", &action);
fgetc(stdin);
while(action == 'Y') {
result = saveToFile(glossary);
if(result < 0) {
printf("Error saving to file! ");
}
else {
printf("Save successful. ");
printf("Exiting the applicaiton. ");
exit(0);
}
printf("Do you want to save your work before exiting? (Y or N)> ");
scanf("%c", &action);
}
printf("Exiting the application. ");
/*L20*/
exit(0);
}

/********************************************************************
DESCRIPTION - addEntry
Adds a new entry to the existing glossary

REQUIREMENTS:
R01 Entries are added in alphabetical order

ALGORITHM:
A100 Prompt user for term, definition;
A105 Search glossary for existing term;
A110 If(term exists) {
Return -1;
}
A115 Else {
Move all terms down in glossary array starting at the last term that was lexically less than the search term;
Add new search term in the now empty array position;
Increment numItems by 1;
Return 0;
}
********************************************************************/

int addEntry(struct item entries[]) { return -1; };

/*L100*/

/********************************************************************
DESCRIPTION - deleteEntry
Delete a term and its associated definition

REQUIREMENTS:
R01 Entries are maintained in alphabetical order

ALGORITHM:
A200 Prompt user for term;
A205 Search glossary for existing term;
A210 If(term exists) {
Delete item from array
Move all items in glossary array up by one position;
Decrement numItems;
Return 0;
}
A215 Else {
Return -1;
}
********************************************************************/

int deleteEntry(struct item entries[]) { return -1; };

/********************************************************************
DESCRIPTION - searchEntry
Search for a term, and display both the term and the definition if found

REQUIREMENTS:
R01 Entries are maintained in alphabetical order

ALGORITHM:
A300 Prompt user for term;
A305 Search glossary for existing term;
A310 If(term exists) {
Display term and definition;
Return 0;
}
A315 Else {
Return -1;
}
********************************************************************/

int searchEntry(struct item entries[]) { return -1; };

char userTerm;

/*L300*/
   printf("Enter term> ");
  
/*L305*/
   scanf("%s", &userTerm);

/*L310*/
   if(

/********************************************************************
DESCRIPTION - readFromFile
Read in terms and definitions

REQUIREMENTS:
R01 Entries are maintained in alphabetical order

R02 Input text file is formatted as provided

R03 File opens without error

ALGORITHM:
A400 Prompt user for filename;
A405 Open file;
A407 If(file open error) {
Return -1;
}
A410 While(!EOF) {
A415 For(each item in file) {
Search for term in existing glossary array
A420 If(item does not exist) {
Add term to glossary array;
Increment numItems;
Continue;
}
A425 Else {
Continue;
}
}
A427 Close file;
A430 Return 0;
********************************************************************/

int readFromFile(struct item entries[]) { return -1; };

/********************************************************************
DESCRIPTION - saveToFile
Save glossary array to file

REQUIREMENTS:
R01 Entries are maintained in alphabetical order

R02 Output text file is formatted as provided

R03 File opens without error

R04 The produced file can be read back in by readFromFile()

ALGORITHM:
A500 Prompt user for filename;
A505 Open file;
A507 If(file open error) {
Return -1;
}
A510 For(each item in glossary array) {
Write to file;
}
A515 Close file;
A520 Return 0;
********************************************************************/


int saveToFile(struct item entries[]) { return -1; };

Explanation / Answer

PROGRAM CODE:

/*
* glosarry.cpp
*
* Created on: 11-Mar-2017
* Author: kasturi
*/

/**Algorithm
---------
A00 Prompt user for selection;
A05 If (valid entry) {
Call corresponding function call;
}
A10 Else {
Prompt again;
}
A15 Prompt user to save work
A20
Return 0;
********************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stddef.h>
/* Definitions */
const int MAX_TERM_LEN = 25;
const int MAX_DEFN_LEN = 255;
const int MAX_ITEMS = 500;
struct item {
char term[25];
char defn[255];
};
/* Function prototypes */
int addEntry(struct item[], int );
int deleteEntry(struct item[], int);
int searchEntry(struct item[], int);
int readFromFile(struct item[], int);
int saveToFile(struct item[], int);
int main() {
/* Declarations */
char action;
int numItems = 0, result = -1;
struct item glossary[MAX_ITEMS];
/*L00*/
/* Prompt loop */
printf("[A]dd, [D]elete, Searc[H], [R]ead from file, [S]ave, [E]nd>");
scanf("%c", &action);
fgetc(stdin);
/*L05*/
while(action != 'E') {
if(action == 'A') {
result = addEntry(glossary, numItems);
if(result < 0) {
printf("Entry exists! ");
}
else {
printf("Add entry successful. ");
}
}
else if(action == 'D') {
result = deleteEntry(glossary, numItems);
if(result < 0) {
printf("Entry does not exist! ");
}
else {
printf("Delete entry successful. ");
}
}
else if(action == 'H') {
result = searchEntry(glossary, numItems);
if(result < 0) {
printf("Entry not found! ");
}
}
else if(action == 'R') {
result = readFromFile(glossary, numItems);
if(result < 0) {
printf("Error reading from file! ");
}
else {
printf("Read successful. ");
}
}
else if(action == 'S') {
result = saveToFile(glossary, numItems);
if(result < 0) {
printf("Error saving to file! ");
}
else {
printf("Save successful. ");
}
}
/*L10*/
printf("[A]dd, [D]elete, Searc[H], [R]ead from file, [S]ave, [E]nd> ");
scanf("%c", &action);
fgetc(stdin);
}
/*L15*/
printf("Do you want to save your work before exiting? (Y or N)> ");
scanf("%c", &action);
fgetc(stdin);
while(action == 'Y') {
result = saveToFile(glossary, numItems);
if(result < 0) {
printf("Error saving to file! ");
}
else {
printf("Save successful. ");
printf("Exiting the application. ");
exit(0);
}
printf("Do you want to save your work before exiting? (Y or N)> ");
scanf("%c", &action);
}
printf("Exiting the application. ");
/*L20*/
exit(0);
}
/********************************************************************
DESCRIPTION - addEntry
Adds a new entry to the existing glossary
REQUIREMENTS:
R01 Entries are added in alphabetical order
ALGORITHM:
A100 Prompt user for term, definition;
A105 Search glossary for existing term;
A110 If(term exists) {
Return -1;
}
A115 Else {
Move all terms down in glossary array starting at the last term that was lexically less than the search term;
Add new search term in the now empty array position;
Increment numItems by 1;
Return 0;
}
********************************************************************/
int addEntry(struct item entries[], int &numItems)
{
   char term[MAX_TERM_LEN], defn[MAX_DEFN_LEN];
   printf("Enter a term: ");
   scanf("%s", term);
   printf(" Enter the definition: ");
   scanf("%s", defn);
   if(numItems == 0)
   {
       strcpy(entries[0].term , term);
       strcpy(entries[0].defn , defn);
       numItems++;
       return 0;
   }
   for(int i=0; i<numItems; i++)
   {
       if(entries[i].term == term)
           return -1;
   }
   for(int i=0; i<numItems; i++)
   {
       if(entries[i].term > term)
       {
           item entry1, entry2;
           entry1 = entries[i];
           strcpy(entries[i].term , term);
           strcpy(entries[i].defn , defn);
           int counter = i+1;
           while(counter < numItems+1)
           {
               entry2 = entries[counter];
               entries[counter] = entry1;
               entry1 = entry2;
           }
       }
       numItems++;
       return 0;
   }
   return -1;

};
/*L100*/
/********************************************************************
DESCRIPTION - deleteEntry
Delete a term and its associated definition
REQUIREMENTS:
R01 Entries are maintained in alphabetical order
ALGORITHM:
A200 Prompt user for term;
A205 Search glossary for existing term;
A210 If(term exists) {
Delete item from array
Move all items in glossary array up by one position;
Decrement numItems;
Return 0;
}
A215 Else {
Return -1;
}
********************************************************************/
int deleteEntry(struct item entries[], int &numItems)
{
   char term[MAX_TERM_LEN];
   printf("Enter a term: ");
   scanf("%s", term);
   for(int i=0; i<numItems; i++)
   {
       if(entries[i].term == term)
       {
           int counter = i;
           while(counter<numItems-1)
           {
               entries[i] = entries[i+1];
           }
           numItems--;
           return 0;
       }
   }

   return -1;
};
/********************************************************************
DESCRIPTION - searchEntry
Search for a term, and display both the term and the definition if found
REQUIREMENTS:
R01 Entries are maintained in alphabetical order
ALGORITHM:
A300 Prompt user for term;
A305 Search glossary for existing term;
A310 If(term exists) {
Display term and definition;
Return 0;
}
A315 Else {
Return -1;
}
********************************************************************/
int searchEntry(struct item entries[], int numItems)
{
   char term[MAX_TERM_LEN];
   printf("Enter a term: ");
   scanf("%s", term);
   for(int i=0; i<numItems; i++)
   {
       if(entries[i].term == term)
       {
           printf("%s : %s", entries[i].term, entries[i].defn);
           return 0;
       }
   }
   return -1;
}

/********************************************************************
DESCRIPTION - readFromFile
Read in terms and definitions
REQUIREMENTS:
R01 Entries are maintained in alphabetical order
R02 Input text file is formatted as provided
R03 File opens without error
ALGORITHM:
A400 Prompt user for filename;
A405 Open file;
A407 If(file open error) {
Return -1;
}
A410 While(!EOF) {
A415 For(each item in file) {
Search for term in existing glossary array
A420 If(item does not exist) {
Add term to glossary array;
Increment numItems;
Continue;
}
A425 Else {
Continue;
}
}
A427 Close file;
A430 Return 0;
********************************************************************/
int readFromFile(struct item entries[], int &numItems)
{
   int isAdded = -1;
   char filename[25], text[MAX_TERM_LEN + MAX_DEFN_LEN + 3];
   printf("Enter the file name: ");
   scanf("%s", filename);
   FILE *in_file = fopen(filename, "r");
   while ((fgets(text, 100, in_file)) != NULL)
   {
       const char *ptr = strchr(text, ':');

       if(ptr) {
       int index = ptr - text;
       char term[MAX_TERM_LEN], defn[MAX_DEFN_LEN];
       int counter = 0;
       while(counter<index)
       {
           term[counter] = text[counter];
           counter++;
       }
       counter = 0;
       while(index<strlen(text))
       {
           defn[counter] = text[index];
           index++;
           counter++;
       }
       int isPresent = -1;
       for(int i=0; i<numItems; i++)
           {
               if(strcmp(entries[i].term, term)==0)
                   isPresent = 0;
           }
       if(isPresent != 0)
       {
           if(numItems == 0)
              {
                  strcpy(entries[0].term , term);
                  strcpy(entries[0].defn , defn);
                  numItems++;
                  isAdded = 0;
              }
           else
           {
               for(int i=0; i<numItems; i++)
                  {
                      if(strcmp(entries[i].term , term)>0)
                      {
                          item entry1, entry2;
                          entry1 = entries[i];
                          strcpy(entries[i].term , term);
                          strcpy(entries[i].defn ,defn);
                          int counter = i+1;
                          while(counter < numItems+1)
                          {
                              entry2 = entries[counter];
                              entries[counter] = entry1;
                              entry1 = entry2;
                          }
                      }
                      isAdded = 0;
                      numItems++;
                  }
           }
       }
       }

   }
   return isAdded;
};
/********************************************************************
DESCRIPTION - saveToFile
Save glossary array to file
REQUIREMENTS:
R01 Entries are maintained in alphabetical order
R02 Output text file is formatted as provided
R03 File opens without error
R04 The produced file can be read back in by readFromFile()
ALGORITHM:
A500 Prompt user for filename;
A505 Open file;
A507 If(file open error) {
Return -1;
}
A510 For(each item in glossary array) {
Write to file;
}
A515 Close file;
A520 Return 0;
********************************************************************/

int saveToFile(struct item entries[], int numItems) {

   char filename[25];
   printf("Enter the file name: ");
   scanf("%s", filename);
   FILE *out_file = fopen(filename, "w");
   for(int i=0; i<numItems; i++)
       fprintf(out_file, "%s : %s ", entries[i].term, entries[i].defn);
   return 0;
};