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

I need help on the extra credit part of my coding class. I did the first part, a

ID: 3765333 • Letter: I

Question

I need help on the extra credit part of my coding class. I did the first part, and he wants these fuctions added to my code:

Assignment:

Extend your previous program to provide additional functionality as follows:

Step (1)

Modify your program to use an array of structs, which will replace the parallel arrays, like this:

// These declarations should be at file scope.

struct HiLoTemp

{

string city;

double low;

double high;

};

const int ARRAY_SIZE = 1000;

HiLoTemp cities [ARRAY_SIZE];

Modify the remainder of the program to work with the new array of structs, but maintaining exactly the same functionality, as well as the added features described below.

Step (2)

Add functionality to sort the array alphabetically by the city name, in ascending or descending order.

Using this new functionality, modify your code so that the output of the Find City menu selection is sorted alphabetically in ascending order by city. Also, remove the Show All menu selection and replace it with the following two menu selections:

Sort (A)scending, Sort (D)escending

You can use any method of sorting you want. You can use a built in sort functions, such as qsort(), or write your own. You may also copy and paste code from a sort routine found online, giving proper credits to the author and/or website.

Sample Dialog

Note: In the example below, the user’s input is in red BOLD. The program output is not.

Welcome to John’s Library Database.

(L)oad File, Sort (A)scending, Sort (D)escending, (F)ind City, (Q)uit: L

Specify the input file path: C: emp emperatures.txt

15 record(s) found.

(L)oad File, Sort (A)scending, Sort (D)escending, (F)ind City, (Q)uit: A

Beijing (45, 66)

Bern (45, 55)

Buenos Aires (69, 84)

Chicago (22, 45)

Cincinnati (21, 39)

Johannesburg (87, 102)

Katmandu (-34, 28)

Miami (68, 84)

Munich (31, 38)

New York (24, 39)

Perth (92, 105)

Seattle (34, 42)

St. Louis (12, 23)

St. Petersburg (19, 37)

Zurich (33, 41)

(L)oad File, Sort (A)scending, Sort (D)escending, (F)ind City, (Q)uit: d

Zurich (33, 41)

St. Petersburg (19, 37)

St. Louis (12, 23)

Seattle (34, 42)

Perth (92, 105)

New York (24, 39)

Munich (31, 38)

Miami (68, 84)

Katmandu (-34, 28)

Johannesburg (87, 102)

Cincinnati (21, 39)

Chicago (22, 45)

Buenos Aires (69, 84)

Bern (45, 55)

Beijing (45, 66)

(L)oad File, Sort (A)scending, Sort (D)escending, (F)ind City, (Q)uit: f

Specify the city search string: c

Chicago (22, 45)

Cincinnati (21, 39)

Munich (31, 38)

Zurich (33, 41)

4 record(s) found.

(L)oad File, Sort (A)scending, Sort (D)escending, (F)ind City, (Q)uit: q

Here is my code thus far:

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <iomanip>

using namespace std;

const char FIELD_DELIM_CHAR = ',';
const char LINE_DELIM_CHAR = ' ';
const int ARRAY_SIZE = 1000;
int fileLen;

string city[ARRAY_SIZE];
char buff1[ARRAY_SIZE];
double lowTemp[ARRAY_SIZE];
double highTemp[ARRAY_SIZE];

int LoadData();
void ShowAll();
int ShowCities(string city[], string str);
void ConvertStringToLowerCase(const string orig, string& lwr);

int i, count;
string filePath;
ifstream inFile;
void main()
{
   cout << "Welcome to Austin's Library Database. " << endl << endl;

   char action;
   string str;
   int data;
   do
   {
       cout << "(L)oad File, (S)how All, (F)ind City, (Q)uit:";
       cin >> action;
       action = tolower(action);

       if (action == 'l')
       {
           cout << "Please enter the name of the temperatures file: ";
           cin >> filePath;
           data = LoadData();

           if (data <= 0) // Prompt user to retry if filepath entered has no values
           {
               cout << "Error opening file! Please try again! " << endl << endl;
           }
           else
           {
               cout << data << " record(s) found." << endl << endl;
           }
       }
       else if (action == 's')
       {
           ShowAll();
       }
       else if (action == 'f')
       {
           cout << ShowCities(city, str) << " record(s) found. " << endl << endl;
       }
       else if (action == 'q')
       {
           break;
       }
       else
       {
           cout << "Invalid Response! Please try again! " << endl << endl;
       }
   } while (action != 'q');
}

int LoadData()
{
   char chr;
   int i = 0;

   // Open the file
   inFile.open(filePath);

   while (!inFile.eof())
   {
       buff1[0] = 0;

       // Get the next field from the file
       inFile.getline(buff1, ARRAY_SIZE, FIELD_DELIM_CHAR);

       // If the array is empty, exit the loop
       if (buff1[0] == 0)
       {
           return -1;
       }
       else if (inFile.eof())
       {
           return i;
       }
       else
       {
           // Convert 'buff' char values into a string
           string s(buff1);
           city[i] = s;

           // Collect low temp value, ignore comma, collect high temp value
           inFile >> lowTemp[i];
           inFile.get(chr);
           inFile >> highTemp[i];
       }
       i++;
   }

   if (inFile.is_open())
   { // Close the file in such a way that it can be reopened
       inFile.close();
       inFile.clear(std::ios_base::goodbit);
   }
   return i; // Return total number of cities listed
}
// Loop to display all data in files
void ShowAll()
{
   for (int i = 0; city[i] != ""; i++)
   {
       cout << " " << city[i] << " (" << lowTemp[i] << ", " << highTemp[i] << ")";
   }
   cout << endl << endl;
}
int ShowCities(string city[], string str)
{
   string lwr1, lwr2;
   int idx, count = 0;

   cout << "City: ";
   cin >> str;

   // Convert a copy of the search string to lower case
   ConvertStringToLowerCase(str, lwr1);

   for (int i = 0; city[i] != ""; i++)
   {
       // Convert a copy of the array string to lower case
       ConvertStringToLowerCase(city[i], lwr2);

       idx = lwr2.find(lwr1);
       if (idx > -1)
       {
           cout << " " << city[i] << " (" << lowTemp[i] << ", " << highTemp[i] << ")";
           count++;
       }
   }
   cout << endl;
   return count; // Return total number of cities with matching strings
}

void ConvertStringToLowerCase(const string orig, string& lwr)
{
   lwr = orig;
   for (int j = 0; j < orig.length(); ++j)
   {
       lwr[j] = tolower(orig.at(j));
   }
}

Explanation / Answer

#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <iomanip>

using namespace std;

const char FIELD_DELIM_CHAR = ',';
const char LINE_DELIM_CHAR = ' ';
const int ARRAY_SIZE = 1000;
int fileLen;

string city[ARRAY_SIZE];
char buff1[ARRAY_SIZE];
double lowTemp[ARRAY_SIZE];
double highTemp[ARRAY_SIZE];

int LoadData();
void ShowAll();
int ShowCities(string city[], string str);
void ConvertStringToLowerCase(const string orig, string& lwr);

int i, count;
string filePath;
ifstream inFile;

void main()
{
    cout << "Welcome to Josh's Library Database. " << endl << endl;

    char action;
    string str;
    int data;
  
     do
        {
            cout << "(L)oad File, (S)how All, (F)ind City, (Q)uit:";
            cin >> action;
            action = tolower(action);
  
            if(action == 'l')
            {
                cout << "Please enter the name of the temperatures file: ";
                cin >> filePath;
                data = LoadData();
  
                if(data <= 0)
                {
                    cout << "Error opening file! Please try again! " << endl << endl;
                }
                else
                {
                    cout << data << " record(s) found." << endl << endl;
                }
            }
            else if(action == 's')
            {
                ShowAll();
            }
            else if(action == 'f')
            {
                cout << ShowCities(city, str) << " record(s) found. " << endl << endl;
            }
            else if(action == 'q')
            {
                break;
            }
            else
            {
                cout << "Please try again! " << endl << endl;
            }
        }while(action != 'q');
    }
  
    int LoadData()
    {
        char chr;
        int i = 0;
  
        // Open the file
        inFile.open(filePath);
  
        while (!inFile.eof())
        {
            buff1[0] = 0;
  
            // Get the next field from the file
            inFile.getline(buff1, ARRAY_SIZE, FIELD_DELIM_CHAR);
  
            // If the array is empty, exit the loop
            if (buff1[0] == 0)
            {
                return -1;
            }
            else if(inFile.eof())
            {
                return i;
            }
            else
            {
                // Convert 'buff' char values into a string
                string s(buff1);
                city[i] = s;
  
                // Collect low temp value, ignore comma, collect high temp value
                inFile >> lowTemp[i];
                inFile.get(chr);
                inFile >> highTemp[i];
            }
            i++;
        }
  
        if (inFile.is_open())
        {   // Close the file in such a way that it can be reopened
            inFile.close();
            inFile.clear(std::ios_base::goodbit);
        }
        return i; // Return total number of cities listed
}

// Loop to display all data in files
void ShowAll()
{
    for(int i = 0; city[i] != ""; i++)
    {
        cout << "   " << city[i] << " (" << lowTemp[i] << ", " << highTemp[i] << ")";
    }
    cout << endl << endl;
}

int ShowCities(string city[], string str)
{
    string lwr1, lwr2;
    int idx, count = 0;

    cout << "City: ";
    cin >> str;

    ConvertStringToLowerCase(str, lwr1);

    for (int i = 0; city[i] != ""; i++)
    {
      
        ConvertStringToLowerCase(city[i], lwr2);

        idx = lwr2.find(lwr1);
        if (idx > -1)
        {
            cout << "   " << city[i] << " (" << lowTemp[i] << ", " << highTemp[i] << ")" << end1;
            count++;
        }
    }
    cout << endl;
    return count; // Return total number of cities with matching strings
}

void ConvertStringToLowerCase(const string orig, string& lwr)
{
    lwr = orig;
    for (int j = 0; j < orig.length(); ++j)
    {
        lwr[j] = tolower(orig.at(j));
    }
}

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