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

Create a menu driven application that recieves input from a textfile “Names.txt”

ID: 3918782 • Letter: C

Question

Create a menu driven application that recieves input from a textfile “Names.txt”, loads the names into an array, and outputs the array back into the textfile. Also provide a function to allow the user to change names.

The application should have the following functions:

void Read( string path, string names []);

Define a function that sequentially reads each name in the textfile and loads them into the array.

void Write( string path, string names[], int SIZE );

Define a function that saves each name from the array into the textfile. Write one name per line.

void ChangeNames(string names [] );

Define a function that allows the user to modify one of the names in the array. First ask the user for the index of the name that needs to be changed, and then ask them for the input.

void DisplayValues(string names [], int SIZE );

Display all the names stored in the array. Please see the screenshot below.

void DisplayMenu();

Display the following text:

Practice Challenge

----------------------------------------------------

1. Load names into the array

2. Display names

3. Change names

4. Save Changes

5. Exit

----------------------------------------------------

Enter a choice[1 – 5]:

int main() should do the following based on this PSEUDOCODE:

1. Create all variables for the filepath, SIZE, names array, and a choice variable.

2. Declare a string array of SIZE 5 called names

3. Display menu, and get choice from the user.

4. Clear Screen - system(“cls”)

5. Based on the user’s choice, call the appropriate function.

--case 1: Call Read function

--case 2: Call DisplayNames fuction

--case 3: Call DisplayNames function and call ChangeNames function

--case 4: Call Write function

--case 5: exit(1)

6. pause screen - system(“pause”)

7. Clear screen

8. While choice does not equal 5, repeat steps 3, 4, 5, 6, and 7

C:windowslsystem32cmd.exe Names [GBooBerry Names [1 ] = Kermit Names [2Piggy ames I31BorisBlade Names L4Chocula Press any key to continue . -

Explanation / Answer

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

using namespace std;

#define SIZE 5

void Read(string path, string names[]){
    ifstream fin(path.c_str());
    if (!fin){
       cout << "Error opening file ";
       return ;
    }
    for (int i = 0; i<SIZE; i++){
        getline(fin,names[i]);
    }
}

void DisplayMenu(){
       cout << "1.Load names into the array ";
       cout << "2.Display names ";
       cout << "3.Change names ";
       cout << "4.Save Changes ";
       cout << "5.Exit ";
       cout << "Enter choice[1-5]:";
}

void DisplayValues(string names[], int n){
   for (int i = 0; i<n; i++){
      cout << "Names[" << i << "] = " << names[i] << endl;
   }
}

void ChangeNames(string names[]){
   cout << "Enter the index:";
   int n;
   string line;
   getline(cin,line);
   n = atoi(line.c_str());
   while (n < 0 || n > 4){
       cout << "Invalid Index ";
       cout << "Enter the index:";
       getline(cin,line);
       n = atoi(line.c_str());
   }
   string name;
   cout << "Enter the new name:";
   getline(cin,name);
   names[n] = name;
}

void Write(string path, string names[], int n){

     ofstream fout(path.c_str());
     for (int i = 0; i<n; i++){
         fout << names[i] << endl;
     }
     fout.close();
}

int main(){

    string names[SIZE];
    while(true){

       DisplayMenu();
       int n;
       string line;
       getline(cin,line);
       n = atoi(line.c_str());
       system("clear");
       if (n == 1){
          cout << "Enter the pathname of the file:";
          string filepath;
          getline(cin,filepath);
          Read(filepath,names);
       }
       else if (n == 2){
           DisplayValues(names,SIZE);
       }
       else if (n == 3){
           DisplayValues(names,SIZE);
           ChangeNames(names);
       }
       else if (n == 4){
          cout << "Enter the path of the output file:";
          string path;
          getline(cin,path);
          Write(path,names,SIZE);
  
       }
       else if (n == 5){
           exit(1);
       }
       else {
          cout << "Invalid choice ";
       }
      
    }
    system("pause");
    system("clear");
    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