//*******************************************************************************************************
//
// File: NameReport.cpp
//
// Student: Anjali Shrestha
//
// Assignment: Program #8
//
// Course Name: Programming II
//
// Course Number: COSC 1560 02
//
// Due: 29th March 2024
//
//
// This program reads from a file.
//
// Other files required:
// 1. ??? (say none if no other files are included)
//
//*******************************************************************************************************
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;
//*******************************************************************************************************
int getChoice ();
void handleMenu (ifstream & );
void displayFirstFive (ifstream & );
void displayLastFive (ifstream & );
int count (ifstream & );
void addNewNames ();
//*******************************************************************************************************
int main()
{
ifstream file;
file.open ("names.txt");
if (file.fail())
{
cout << "Cannot open file" << endl;
}
else
{
handleMenu(file);
file.close();
}
return 0;
}
//*******************************************************************************************************
int getChoice ()
{
int choice;
do
{
cout << string (40, '*') << endl;
cout << "1. Display first five names" << endl
<< "2. Display last five names" << endl
<< "3. Count the number of names" << endl
<< "4. Add a new name" << endl
<< "5. Quit" << endl;
cout << string (40, '*') << endl;
cout << "\nEnter your choice: ";
cin >> choice;
}
while (choice < 1 || choice > 5);
return choice;
}
//*******************************************************************************************************
void handleMenu (ifstream & fin)
{
const int DISPLAY_FIRST_FIVE = 1,
DISPLAY_LAST_FIVE = 2,
COUNT = 3,
ADD_NEW_NAMES = 4,
QUIT = 5;
bool quit = false;
while (!quit)
{
int choice = getChoice();
switch (choice)
{
case DISPLAY_FIRST_FIVE:
displayFirstFive(fin);
cout << endl;
break;
case DISPLAY_LAST_FIVE:
displayLastFive(fin);
cout << endl;
break;
case COUNT:
cout << "There are " << count(fin) << " names" << endl;
cout << endl;
break;
case ADD_NEW_NAMES:
addNewNames();
cout << endl;
break;
case QUIT:
quit = true;
cout << "Over!" << endl;
break;
default:
cout << "!!! Choice out of range 1 - 5" << endl;
break;
}
}
}
//*******************************************************************************************************
void displayFirstFive (ifstream & fin)
{
fin.clear();
fin.seekg(0L, ios::beg);
char names[81];
for (int i = 0; i < 5; i++)
{
fin.getline(names, 81);
cout << i + 1 << ". " << names << endl;
}
}
//*******************************************************************************************************
int count(ifstream & fin)
{
fin.clear();
fin.seekg(0L, ios::beg);
int count = 0;
char names[81];
while (fin.getline(names, 81))
{
count++;
}
return count;
}
//*******************************************************************************************************
void displayLastFive(ifstream & fin)
{
fin.clear();
fin.seekg(0L, ios::beg);
char names[81];
int totalNumberOfLines = count(fin);
for (int i = 0; i < totalNumberOfLines; i++)
{
fin.getline(names, 81);
if ( i >= totalNumberOfLines - 5)
cout << i << ". " << names << endl;
}
}
//*******************************************************************************************************
void addNewNames()
{
ofstream fout;
fout.open("names.txt", ios::app);
char names[81];
if (fout.fail())
{
cout << "Cannot open file." << endl;
}
else
{
cout << "Adding a name" << endl;
cout << "Enter a name: ";
cin.ignore();
cin.get (names, 81);
fout << names << endl;
fout.close();
}
}
//*******************************************************************************************************
// attach your sample output here.