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

Create a menu system that read, writes and appends to a text file. You do not ne

ID: 3682517 • Letter: C

Question

Create a menu system that read, writes and appends to a text file.

You do not need to use arrays program. You need to use functions as appropriate.

The idea is to manage multiple lists of names stored on the disk. You can display and append any list of names stored from a main menu.

Main Menu:

Display the names in a list.

Add a name to a list.

[optional] Show all the lists.

Exit.

The program will display 'invalid option' if something not on the menu is selected.

If a valid option is selected, the program will do that option,

The program will continue to ask for choices until 'Exit' is selected, then the program will end.

If you ask for numbers as the options, I will only type in numbers. (cin >> nuInteger; has issues with letters, but that's not out problem)

Remember that getline() and cin >> do not always work well together!

Display the names in a list

When this option is selected the program will prompt for a file name. The program will then open the file and display the content of that file, 20 items at a time with a pause after the 20 names. You may assume that only text files will be displayed.

Allow the user to put both the filename and the extension, like party1.txt or mynames.list

If that file does not exist, you must display a message saying it doesn't exist, then go back to the menu.

If the file does exist, display the contents of the file 20 items at a time with a pause after the 20 names.

Add a name to a list

When this option is selected the program will prompt for a file name AND then prompt for a name. Names always contain an unknown amount of spaces, (see getline).

Allow the user to put both the filename and the extensions, like party1.txt or mynames.list

If the file does exist, the name will be APPENDED to the end of the file.

If that file does not exist, it will be created. (This will happen anyway with the append selected)

The user may put in any name with any number of spaces. (use getline, not >>)

Exit

When this option is pressed, the program ends. It should not end for any other reason. (Of course you can't control closing the window and things like that)

[optional] Show all the lists.


When this option is pressed, you can display the contents of the directory with all files ending in .txt and .list

Explanation / Answer

Here is the program

#include <fstream>
#include <iostream>
#include<string>
#include<dirent.h>


using namespace std;

void displayName();
void addName();
void showList();
void menu()
{
   int option=1;
   while(true)
   {
      
       cout << " Enter choice... 1 Display the names in a list 2 Add name to list 3 to Show lists 0 Exit ";
       cin >> option;
       if(option==1)
       displayName();
   else if(option==2)
           addName();
          
   else if(option==3)
       showList();
        else if(option==0)
            break;
   }
}
void displayName()
{
   char filename[100];
   string line;
   cout << "Enter filename ";
   cin >> filename;
   if (std::ifstream(filename))
   {
   int i=0,j=0;
       ifstream infile;
   infile.open(filename);
   cout << "Reading from the file " << filename << endl;
   while (std::getline(infile, line))
       {
           i++; j++;
           cout << j <<": "<< line << endl;
          
          
       }
      
       infile.close();
   }
   else cout << "File Doesnot exist ";
  
}
void addName()
{
   char filename[100];
   string name;
  
   cout << "Enter list ";
   cin >> filename;
   cout << "Enter name ";
   cin >> name;
   ofstream outfile;
outfile.open(filename,ios::out | ios::app );
outfile << name << endl;
}
void showList()
{
   DIR *dir;
   struct dirent *ent;
   if ((dir = opendir (".")) != NULL) {
   /* print all the files within directory */
   while ((ent = readdir (dir)) != NULL) {
   cout << ent->d_name<< endl ;
   }
   closedir (dir);
   } else {
   cout << "Error";
   }
}
int main ()
{
  
menu();

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote