/*
* Description:
C++ program that processes a string data file. The program
1) reads the names from a string data file and place them in an Vector
2) sorts the names in the Vector in ascending order using selection sort.
The program uses a function to display the Vector before it is sorted and AFTER it is sorted.
A separate function is implemented to save the sorted names to a data file
*/
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
// function reads names from file to store them into vector
vector<string> readTheNamesFromTheDataFile(string);
// function displays a vector
void displayTheVector(const vector<string> &);
// function sorts the string vector
void sortTheVectorAscending(vector<string> &);
// function saves the string vector into the file
void saveVectorToFile(const vector<string>&, string);
int main() {
cout << "Please enter the names of the file to read names from: " << endl;
string fileName;
cin >> fileName;
auto names = readTheNamesFromTheDataFile(fileName);
if (names.size()) {
cout << "\nHere are the unsorted names:\n";
cout << "------------------------------\n";
displayTheVector(names);
sortTheVectorAscending(names);
cout << "\nHere are the names as sorted:\n";
cout << "------------------------------\n";
displayTheVector(names);
cout << "\nPlease enter the names of the file to save sorted names to: " << endl;
cout << "------------------------------\n";
string fileNameForOutput;
cin >> fileNameForOutput;
saveVectorToFile(names, fileNameForOutput);
}
else {
cout << "Names list is empty!\n";
}
system("PAUSE");
}
vector<string> readTheNamesFromTheDataFile(string fileName)
{
ifstream file;
file.open(fileName);
vector<string> result;
if (file.is_open()) {
while (!file.eof())
{
string newName;
getline(file, newName);
result.push_back(newName);
}
file.close();
}
else {
cout << "File " << fileName << " doesn't exist\n";
}
return result;
}
void displayTheVector(const vector<string> &names)
{
for (size_t i = 0; i < names.size(); i++)
{
cout << names[i] << "\n";
}
}
void saveVectorToFile(const vector<string> & names, string fileName)
{
ofstream fout(fileName);
for (size_t i = 0; i < names.size(); i++)
{
fout << names[i] << "\n";
}
fout.close();
}
void sortTheVectorAscending(vector<string> & names)
{
for (size_t i = 0; i < names.size(); i++)
{
int minIdx = i;
for (size_t j = i + 1; j < names.size(); j++) {
if (names[j] < names[minIdx]) {
minIdx = j;
}
}
swap(names[i], names[minIdx]);
}
}
/*
Program output:
Please enter the names of the file to read names from:
names.txt
Here are the unsorted names:
------------------------------
Collins, Bill
Smith, Bart
Allen, Jim,
Griffin, Jim
Stamey, Marty
Rose, Geri,
Taylor, Terri
Johnson, Jill,
Allison, Jeff
Looney, Joe
Wolfe, Bill,
James, Jean
Weaver, Jim
Pore, Bob,
Rutherford, Greg
Javens, Renee,
Harrison, Rose
Setzer, Cathy,
Pike, Gordon
Holland, Beth
Here are the names as sorted:
------------------------------
Allen, Jim,
Allison, Jeff
Collins, Bill
Griffin, Jim
Harrison, Rose
Holland, Beth
James, Jean
Javens, Renee,
Johnson, Jill,
Looney, Joe
Pike, Gordon
Pore, Bob,
Rose, Geri,
Rutherford, Greg
Setzer, Cathy,
Smith, Bart
Stamey, Marty
Taylor, Terri
Weaver, Jim
Wolfe, Bill,
Please enter the names of the file to save sorted names to:
------------------------------
namesOutput_SortedNames.txt
*/
Comments
Dear visitor, please use panel for submitting new questions
Can this problem be done without using vector?
Leave a comment