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

In my c++ assignment, I wrote the following code, which works. To get full credi

ID: 3690087 • Letter: I

Question

In my c++ assignment, I wrote the following code, which works. To get full credit, I have to convert to called functions, which I have difficulty with. The file contains: Turtle Hatchling Oyster Spat Jellyfish Ephyna Sand-Dollar Pluteus Porpoise Calf Trout Fry Manatee Calf Shark Pup Fish Fry The working code is: #include #include #include #include #include #include // Needed to use the sort function http://stackoverflow.com/questions/18292619/c-string-array-sorting using namespace std; // Function Prototypes //*************************************************************************** void welcome(); // Introduces program 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 filename, filename2; do { // Insert program between lines //************************************************************************************************************ const int size = 9; // Get filename cout << "Enter filename:" << endl; cin >> filename; system("cls"); // Clears screen // Get file data inputFile.open(filename); if (inputFile) // File varification { 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 // Print to screen cout << endl << " The following are the sorted names: " << endl; // Sort // I was havving difficultyy in adapting the sort algorthym in the textbook, // so I resorted to the Internet: http://stackoverflow.com/questions/18292619/c-string-array-sorting int z = sizeof(animalNames) / sizeof(animalNames[0]); //Get the array size sort(animalNames, animalNames + z); //Use the start and end like this for (int y = 0; y < z; y++) { cout << animalNames[y] << endl; } // Write to file cout << " Enter filename to save data:" << endl; cin >> filename2; ofstream outputFile; outputFile.open(filename2); if (inputFile) // File verificaton { for (int count = 0; count < size; count++) outputFile << animalNames[count] << endl; outputFile.close(); // Close the file } else { cout << "Error opening file." << endl; } } else { cout << "Error opening file." << endl; } //************************************************************************************************************ // 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 a text file, sort the file" << endl; cout << "sort the file alphabetically, and write to a file. " << endl; cout << "Program Developed By: Carl Clark" << endl; cout << "*********************************************************** " << endl; }

Explanation / Answer

#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <fstream>
using namespace std;
// Function Prototypes //***************************************************************************
void welcome();
// Introduces program
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 filename, filename2;

do
{
// Insert program between lines //************************************************************************************************************
const int size = 9;
// Get filename
cout << "Enter filename:" << endl;
cin >> filename;
//system("cls"); // Clears screen
// Get file data
inputFile.open(filename.c_str());
if (inputFile)
// File varification
{
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
// Print to screen
cout << endl << " The following are the sorted names: " << endl;
// Sort
sort(animalNames,animalNames+size);
int z = sizeof(animalNames) / sizeof(animalNames[0]);
//Get the array size sort(animalNames, animalNames + z);
//Use the start and end like this
for (int y = 0; y < z; y++)
{
cout << animalNames[y] << endl;
}
// Write to file
cout << " Enter filename to save data:" << endl;
cin >> filename2;
ofstream outputFile;
outputFile.open(filename2.c_str());

if (inputFile)
// File verificaton
{
for (int count = 0; count < size; count++)
outputFile << animalNames[count] << endl;

outputFile.close(); // Close the file
}
else
{
cout << "Error opening file." << endl;
}
}
  
else
{
cout << "Error opening file." << endl;
}

//************************************************************************************************************
// 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 a text file, sort the file" << endl;
cout << "sort the file alphabetically, and write to a file. " << endl;
cout << "Program Developed By: Carl Clark" << endl;
cout << "*********************************************************** " << endl;
}

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