I created this for a project at class, and I already turned it in so I know it's too late for changing it and whatnot as it's the end of the semester, but I want to know from the community how well I did. Don't judge all the std::
stuff. I built this using using namespace std;
.
#include <iostream>
#include <fstream>
#include <vector>
using std::cout;
using std::cin;
using std::string;
using std::ifstream;
using std::iostream;
using std::endl;
using std::ofstream;
void openFile(){
string getcontent;
ifstream openfile ("namelist.txt");
if(openfile.is_open())
{
while(getline(openfile, getcontent))
{
cout << getcontent << endl;
}
}
}
void addName(){
string name;
cout << "Please type a name you would like to add:";
ofstream outfile;
outfile.open("namelist.txt", std::ios_base::app);
cin >> name;
outfile << name << endl;
cout << "Name added!" << endl;
outfile.close();
}
int main(int argc, const char * argv[]) {
char ch;
while(1) {
/// print the menu
cout << "Menu Lab v1" << endl;
cout << "--------------" << endl;
cout << endl;
cout << "A) Display Names in list" << endl;
cout << "B) Add a name to a list" << endl;
cout << "C) Exit" << endl;
cout << endl;
cout << "Enter your choice:";
cin >> ch;
cout << endl;
/// do something based on the choice
switch (ch)
{
case 'a':
case 'A':
openFile();
break;
case 'b':
case 'B':
addName();
break;
case 'c':
case 'C':
exit(1);
break;
default:
cout << "You entered an invalid option" << endl;
}
cout << endl;
cout << endl;
}
return 0;
}