64 604
Assignments Done
99,1%
Successfully Done
In September 2018

Answer to Question #71515 in C++ for niru

Question #71515
Design, implement and test a C++ program which will process a list of names, sort them using selection sort, and save the output to a file.
Requirements
Process a data file of “student” names. See provided sample file
Include a function to sort the names in ascending or descending order using selection sort
Include another function to display the names BEFORE as well as AFTER it is sorted
Include yet another function to save the sorted names to a data file named Output_SortedNames.txt.”
Including main (), 4 or more functions must be implemented. Proper prototypes should be utilized and function bodies must be coded after the main function
Assume that the input data file will contain between 20 and 50 names where each name is written on a separate line and is constructed correctly

Input Validations –
“Reasonable” input and output validations are expected for this project
For example, if there is an error while attempting to open the input file, display an appropriate error message
Expert's answer
/*
* 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
*/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
06.12.17, 19:00

Dear visitor,
please use panel for submitting new questions

niru
06.12.17, 04:49

Can this problem be done without using vector?

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions