Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do you sort a string array in c++? That is, I read a list of names from a fi

ID: 3689998 • Letter: H

Question

How do you sort a string array in c++? That is, I read a list of names from a file, and then must alphabetize them, and then write them to a file. I must use a selction sort algorithm -- I'm in Intro to Programminng.

The file contains the following lines (each line is to remain to gether):

Turtle Hatchling
Oyster Spat
Jellyfish Ephyna
Sand-Dollar Pluteus
Porpoise Calf
Trout Fry
Manatee Calf
Shark Pup
Fish Fry

This is what I have, but it doesn't work...

#include <iostream>

#include <cstdlib>

#include <iomanip>

#include <string>

#include <fstream>

using namespace std;

// Function Prototypes

//***************************************************************************

void welcome(); // Introduces program

void selectionSort(string[], int);

void showArray(string[], int);

const int size = 9;

int main()

{

// Introduction not part of repetive loop

welcome(); // Welcome Screen

system("pause"); // "Press any key to contiue" program pause

system("cls"); // Clears screen

// variables

char again; // To hold y or n

ifstream inputFile;

string name;

string filename, filename2;

do

{

// Insert program between lines

//************************************************************************************************************

// Insert functions and statments here for actual program

// Get file name

cout << "Enter filename:" << endl;

cin >> filename;

filename = "animals.txt"; // defines the file location, because typing in the file didn't work

system("cls"); // Clears screen

// Get file data

inputFile.open(filename);

cout << "The following are the unsorted names from the file: " << endl;

string animalNames[size];

for (int count = 0; count < size; count++)

{

getline(inputFile, animalNames[count], ' '); // Read names from the file

cout << animalNames[count] << endl; // Display names

}

inputFile.close(); // Close the file

// Sort

animalNames[count];

cout << ' ';

// Print to screen

cout << "The sorted names are: " << endl;

showArray(animalNames, size);

// Write to file

cout << "Enter filename to save data:" << endl;

cin >> filename2;

filename2 = "results.txt"; // defines the file location, because typing in the file didn't work

ofstream outputFile;

outputFile.open(filename2);

for (int count = 0; count < size; count++)

outputFile << animalNames[count] << endl;

outputFile.close(); // Close the file

//************************************************************************************************************

// Use again

cout << endl << "Do you want to start over? (y/n) ";

cin >> again;

system("cls"); // Clears screen

} while (again == 'Y' || again == 'y');

return 0;

}

// /functions are below the line

//***********************************************************************************************************

void welcome()

{

cout << "***********************************************************" << endl;

cout << "This program will read the file animals.txt:" << endl;

cout << "Sea Animals and Their Baby Names " << endl;

cout << "Program Developed By: Carl Clark" << endl;

cout << "*********************************************************** " << endl;

}

//**************************************************************

// Definition of function showArray. *

// This function displays the contents of array. size is the *

// number of elements. *

//**************************************************************

void showArray(string array[], int size)

{

for (int count = 0; count < size; count++)

cout << animalNames[count] << " ";

cout << endl;

}

void selectionSort(string array[], int size)

{

int startScan, minIndex, minValue;

for (startScan = 0; startScan < (size - 1); startScan++)

{

minIndex = startScan;

minValue = animalNames[startScan];

for (int index = startScan + 1; index < size; index++)

{

if (animalNames[index] < minValue)

{

minValue = animalNames[index];

minIndex = index;

}

}

animalNames[minIndex] = animalNames[startScan];

animalNames[startScan] = minValue;

}

}

Explanation / Answer

Instead of this long tedious sorting you use the following simple sort method:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
string name[] = {"Turtle Hatchling","Oyster Spat","Jellyfish Ephyna","Sand-Dollar Pluteus","Porpoise Calf","Trout Fry","Manatee Calf","Shark Pup","Fish Fry"};

int z = sizeof(name)/sizeof(name[0]); //Get the array size

sort(name,name+z); //Use the start and end like this

for(int y = 0; y < z; y++){
cout << name[y] << endl;
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote