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: 3690110 • 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 workinng program that needs to be converted to functions:

#include <iostream>

#include <cstdlib>

#include <iomanip>

#include <string>

#include <fstream>

#include <algorithm> // Needed to use the sort function http://stackoverflow.com/questions/18292619/c-string-array-sorting

using namespace std;

// Function Prototypes

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

void welcome(); // Introduces program

void showArray(string myArray[], 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 filename, filename2;

       do

       {

              // Insert program between lines

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

             

       // Get filename

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

              cin >> filename;

              system("cls"); // Clears screen

              string animalNames[SIZE];

                                     // 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

                     {

                           outputFile << "Sea Animals and Their Baby's Names" << endl << endl;

                           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;

}

// 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

{

outputFile << "Sea Animals and Their Baby's Names" << endl << endl;

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