/* Program creates a user-named .txt file (if file does not exist)
in which user can build a list of words up to user-chosen
number of entries desired. Console then displays all entries in
.txt If same file is reopened, the list is appended from the end. */
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string filename; // creates string for user input
cout << "Input file name to open: ";
cin >> filename;
cout << endl;
fstream myfile(filename.c_str(), ios::out | ios::app); // declares fstream class "myfile". Opens file from user input.
if (myfile.is_open())
{
int x = 0;
cout << "Enter the number of words you would like to enter: ";
cin >> x;
cout << endl;
string input; // User controlled for-loop from x
for (int i = 0;i < x; i++)
{
cout << "Enter word " << i <<":";
cin >> input;
myfile << input << endl;
}
cout << endl;
myfile.close();
}
else
cout << "Unable to open file!" << endl;
//-------------------------------------------------------------------------------------------------------
string line; // creates string to read line from .txt
myfile.open("example.txt", ios::in); // Opens created file (myfile declared on line 14)
if (myfile.is_open())
{
int numPlace = 1;
cout << "The list is:" << endl;
while (!myfile.eof()) // while-loop to print what is in .txt aka(myfile.good())
{
getline (myfile, line);
cout << numPlace << ". "
<< line << endl;
numPlace++;
}
myfile.close();
}
else
cout << "Unable to open file!" << endl;
return 0;
}