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

Could someone help me fix my code. When I run it, its just a endless rerun of th

ID: 3568766 • Letter: C

Question

Could someone help me fix my code. When I run it, its just a endless rerun of the whole program.
Ive added the prompt at the bottom of the page. Thanks in advance.



#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;


void show()
{
string file_nm;
cout<<"Enter file name";
cin>>file_nm;
string STRING;
ifstream infile;

if (infile.good())
{
infile.open ("file_nm");
while(!infile.eof())
{
getline(infile,STRING);
cout<<STRING;
}

infile.close();
}
else
{
cout<< "File doesnot exit";
}

cin.get();

}

void add()
{
string txt, file_nm;
cout<<"Enter file name";
cin>> file_nm;
cout<<"enter text to be added";
cin>> txt;
std::ofstream outfile;
outfile.open(file_nm.c_str());
outfile << "ENTRY";
}

int main(int argc, char *argv[])

{
int l = 1;
while(1)
{
cout << "Main menu ";
cout << " 1) Show a list ";
cout << " 2) Add a name ";
cout << " 3) exit ";
}
}

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

You do not need to use arrays in this lab. You need to use functions as appropriate. This lab is not counted in the midterm grade.

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:

Show 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)

Show 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 contents of that file. You may assume that only text files will be displayed.

If that file does not exist, you must display a message saying it doesn't exist.

If the file does exist, display the contents of the file.

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

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).

If that file does not exist, it will be created.

If the file does exist, the name will be APPENDED to the end of the file. (see ios::app)

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

All the user to put in any name with any number of spaces.

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, Extra Credit] 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

This option is system specific, windows stuff won't work on linux, apple stuff wont work on wondows, etc. Just put in the message area if you did this and what system you're using and I'll deal with it accordingly.

As an example:

1) Show a list
2) Add a name to a list
3) Exit

Enter Choice: 1

Enter File Name: names.txt

name one
name two
name three

1) Show a list
2) Add a name to a list
3) Exit

Enter Choice: 2

Enter File Name: names.txt

Enter Name: another name

1) Show a list
2) Add a name to a list
3) Exit

Enter Choice: 1

Enter File Name: names.txt

name one
name two
name three
another name

1) Show a list
2) Add a name to a list
3) Exit

Enter Choice: 1

Enter File Name: names.text

File does not exist

1) Show a list
2) Add a name to a list
3) Exit

Enter Choice: 3

Good-bye

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

void show() // this function displays the file content
{ string file_nm;
cout<<"Enter file name";
cin>>file_nm;
string STRING;
ifstream infile;
if (file.good())
{ infile.open ("file_nm");
while(!infile.eof) // To get you all the lines.
{
   getline(infile,STRING); // Saves the line in STRING.
   cout<<STRING; // Prints our STRING.
}
infile.close();
}
else { cout<< "File doesnot exit"; }
getch();
}


void add() // Appends to the end of the file
{
string txt, file_nm;
cout<<"Enter file name";
cin>> file_nm;
cout<<"enter text to be added";
cin>> txt;
std::ofstream outfile;
outfile.open(file_nm, std::ios_base::app);
outfile <<ENTRY;
}

/***************************************MAIN PROGRAM************************************************/
int main()
{ int l;
while(1)
{
cout<<"Main menu 1) Show a list 2) Add a name 3) exit ";
switch(l)
{
case 1: show(); continue;
case 2: add(); continue;
case 3: exit(1);
default: cout<<"Invalid option";
}

}

return 0;
}