1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void readFile(string, vector<string>, ifstream &); //Function prototype for the reading the file function
void displayNames(vector<string>); //Function prototype for the displaying the names function
void addName(string, ofstream &, vector<string>); //Funtion protoype for the adding a name to the list function
void deleteName(string, ofstream &, vector<string>); //Function prototype for the deleting a name from the list function
void quitProgram(); //Funtion prototype for the exiting function
int main()
{
char cInput; //Menu input
ifstream inFile; //Input file
ofstream outFile; //Outut file
string strFileName; //File name
vector<string> vecStudent; //Vector holding student names
cout << "Please enter the data file name (with location): "; //Asks the user for a file location and the name of file
cin >> strFileName; //User enters the file location and name
while (inFile.fail()) //While the file does not open display error message and asks for another file name and location
{
cout << "--------------------------------------------------\n";
cout << "Input file error!\n";
cout << "Please enter the data file name (with location): ";
cin >> strFileName;
}
readFile(strFileName, vecStudent, inFile); //Calls readFile function to read the file into the vector
while (true)
{
cout << "----------------------------------------\n";
cout << " Grade Report Program - Main Menu\n";
cout << "----------------------------------------\n";
cout << " Enter 1 to display ALL students names\n";
cout << " Enter 2 to add a student name\n";
cout << " Enter 3 to delete a student name\n";
cout << " Enter 4 to SAVE and quit the program\n";
cout << "----------------------------------------\n";
cout << "Enter menu option: "; //Asks user for menu option
cin >> cInput; //User inputs menu option
switch (cInput)
{
case '1':
displayNames(vecStudent); //call a function to handle this option
break;
case '2':
addName(strFileName, outFile, vecStudent); //call a function to handle this option
break;
case '3':
deleteName(strFileName, outFile, vecStudent);//call a function to handle this option
break;
case '4':
quitProgram();//call a function to handle this option
return 0;
default:
cout << "invalid input" << endl;
break;
}
}
return 0; //Return 0 to exit program
}
void readFile(string strFile, vector<string> vecNames, ifstream &iFile) //Read the file into the vector function definition
{
string strFName, strLName; //First and last name
iFile.open(strFile.c_str()); //Opens file
while (iFile >> strFName >> strLName) //While the file is copying into the first and last names
{
vecNames.push_back(strFName + " " + strLName); //Push the names onto the back of the of the vector
}
iFile.close(); //Close the input file
}
void displayNames(vector<string> vecNames) //Display the names funtion definition
{
cout << "--------------------------------------------------\n";
cout << "Display all student names...\n";
for (int i = 0; i < vecNames.size(); i++)
cout << vecNames[i] << endl; //Displays the names from the vector
cout << "---- A total of " << vecNames.size() << " names ----\n";
}
void addName(string strFile, ofstream &oFile, vector<string> vecNames) //Add the name function definition
{
string strFName; //First name
string strLName; //Last name
oFile.open(strFile.c_str()); //Opens the output file
while (oFile.fail()) //While the output file does not open display error message and prompt user again for a first and last name
{
cout << "----------------------------------------\n";
cout << "Output file error!\n";
cout << "Enter the name to be added <First and Last Name>: ";
cin >> strFName >> strLName;
oFile.open(strFile.c_str()); //Tries to open file again
}
cout << "----------------------------------------\n";
cout << "Enter the name to be added <First and Last Name>: "; //Asks user for the first and last name to be added
cin >> strFName >> strLName; //User enters first and last name to be added
vecNames.push_back(strFName+" "+strLName); //Adds the name entered into the vector
for (int i = 0; i < vecNames.size(); i++)
oFile << vecNames[i] << endl; //Put the names from the vector into the output file
oFile.close(); //Closes the output file
cout << "---- Name '" << strFName << " " << strLName << "' has been added.\n"; //Displays that the name has been added
}
void deleteName(string strFile, ofstream &oFile, vector<string> vecNames) //Delete the name funtion definition
{
string strFName; //Initialize first name variable
string strLName; //Initialize last name variable
cout << "----------------------------------------\n";
cout << "Enter the name to be deleted <First and Last Name>: "; //Ask use what name they want to delete
cin >> strFName >> strLName;
vecNames.pop_back(); //Take that name off of the vector
oFile.open(strFile.c_str()); //Open the output file
while (oFile.fail()) //While the output while does not open display and error message and ask the user again
{
cout << "----------------------------------------\n";
cout << "Output file error!\n";
cout << "Enter the name to be deleted <First and Last Name>: ";
cin >> strFName >> strLName;
oFile.open(strFile.c_str());
}
for (int i = 0; i < vecNames.size(); i++)
oFile << vecNames[i] << endl; //Put the names from the vector into the output file
oFile.close(); //Close the output file
cout << "---- Name '" << strFName << " " << strLName << "' has been deleted.\n";
}
void quitProgram() //Quitting the program function definition
{
cout << "--------------------------------------------------\n";
cout << "Thanks for using the program. Program terminated.\n"; //Displays that the program has terminated
}
|