leialeia
leialeia

Reputation: 21

Is there a reason my program is incapable of sorting corresponding char arrays?

The task is to create a program that is capable of sorting patient records based on a couple of different categories. I am trying to have it sort by age, which it can do, and have the corresponding values line up in the same order.

It can output ID, lastName, and firstName in the correct order, but when I try to make it output sex the same way, it tells me that an argument of type char is incompatible with parameter of type const char *. I'm not sure why exactly sex would be any different from the other variables.

#include <iostream>
#include <cstring> // required for string manipulation
#include <iomanip> // required for tabular output

using namespace std; 

int main()
{
    // Provided arrays and constants
    string category;
    int hold;
    char holdstr[10];
    char holdstr2[10];
    char holdstr3[10];
    char holdstr4[10];
    char holdsex[1];
    const int NUM_PATIENTS = 10; // 10 patients will be included, this value cannot be modified
    const int MAX_NAME_LENGTH = 10; // no names longer than 10 characters, this value cannot be modified
    const int ID_LENGTH = 8;
    unsigned int age[NUM_PATIENTS] = {50, 22, 22, 89, 15, 16, 22, 32, 15, 66}; // 1D int array age of each patient
    char id[NUM_PATIENTS][ID_LENGTH + 1] = {"10854893", "10983922", "98333894", "57290888", "14899822", "79278282", "44293822", "62911023", "28399020", "33212322"}; // 1D int array identification #
    char firstName[NUM_PATIENTS][MAX_NAME_LENGTH] = {"allison", "michael", "michael", "johnny", "sabrina", "helen", "ignacio", "melissa", "hassan", "melody"}; // 2D char array: first names for patients
    char lastName[NUM_PATIENTS][MAX_NAME_LENGTH] = {"pratt","xiao", "barkley", "wu", "sappal", "mcdonald", "garza", "tran", "nahas", "baker"}; // 2D char array: last names for patients
    char sex[10][2] = {'f','m','m','m','f','f','m','f','m','f'}; // 1D char array sex of the patient
    //

    // PART 1: Select a primary sorting category from user input
beginning:{
cout << "Please enter the sorting category (Age, ID, First, Last, Sex" << endl;
cin >> category; //collect required category

for (int i=0 ; i <category.size() ; ++i) category[i] = toupper( category[i]); //convert to upper case no matter what
if (category=="AGE")
{
    cout << "Case 1, sorting by age." << endl;
    for (int i=0; i<10; i++)
    {
        for (int j=0; j<10; j++)
        {
            if (age[j]>age [j+1]||age[j]==age[j+1])
            {
                hold=age[j];
                strcpy(holdstr, firstName[j]);
                strcpy(holdstr2,lastName[j]);
                strcpy(holdstr3, id[j]);

                age [j]=age [j+1];
                strcpy(firstName[j], firstName[j+1]);
                strcpy(lastName [j], lastName [j+1]);
                strcpy(id[j],id[j+1]);


                age [j+1]=hold;
                strcpy(firstName[j+1], holdstr);
                strcpy(lastName[j+1],holdstr2);
                strcpy(id[j+1],holdstr3);
            }
        }
    }
}
else if (category=="ID")
{
    cout << "Case 2, sorting by ID."<< endl;
}
else if (category=="FIRST")
{
    cout << "Case 3, sorting by first name." << endl;
}
else if (category=="LAST")
{
    cout << "Case 4, sorting by last name." << endl;
}
else if (category=="SEX")
{
    cout << "Case 5, sorting by sex" << endl;
}
else
{
    cout << "Invalid input" << endl;
    cin.clear();
    cin.ignore(100,'\n');
    goto beginning;
}


    // PART 3/4: Sorting patient records


    // PART 2: Outputting patient records to terminal in tabular form
cout << setw(10) << "Age" << setw(10) << age [0] << setw(10) << age [1]<< setw(10)<< age [2]<< setw(10) << age [3] << setw(10) << age [4] << setw(10) << age [5] << setw(10)<< age [6] << setw(10)<< age [7] << setw(10) << age [8] << setw(10) << age [9] << setw(10) <<endl;
cout << setw(10) << "ID"<< setw(10) << id [0] << setw(10) << id [1]<< setw(10)<< id [2]<< setw(10) << id [3] << setw(10) << id [4] << setw(10) << id [5] << setw(10)<< id [6] << setw(10)<< id [7] << setw(10) << id [8] << setw(10) << id [9] << setw(10) <<endl;
cout << setw(10) << "First Name" << setw(10) << firstName [0] << setw(10) << firstName [1]<< setw(10)<< firstName [2]<< setw(10) << firstName [3] << setw(10) << firstName [4] << setw(10) << firstName [5] << setw(10)<< firstName [6] << setw(10)<< firstName [7] << setw(10) << firstName [8] << setw(10) << firstName [9] << setw(10) <<endl;
cout << setw(10) << "Last Name" << setw(10) << lastName [0] << setw(10) << lastName [1]<< setw(10)<< lastName [2]<< setw(10) << lastName [3] << setw(10) << lastName [4] << setw(10) << lastName [5] << setw(10)<< lastName [6] << setw(10)<< lastName [7] << setw(10) << lastName [8] << setw(10) << lastName [9] << setw(10) <<endl;
cout << setw(10) << "Sex" << setw(10) << sex [0] << setw(10) << sex [1]<< setw(10)<< sex [2]<< setw(10) << sex [3] << setw(10) << sex [4] << setw(10) << sex [5] << setw(10)<< sex [6] << setw(10)<< sex[7] << setw(10) << sex [8] << setw(10) << sex [9] << setw(10) <<endl;
return 0;
}
}

Upvotes: 0

Views: 60

Answers (1)

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33864

You have attempted this using a Structure of Arrays approach. You should aim for an Array of Structures approach. From wikipedia:

Array of structures (AoS) is the opposite (and more conventional) layout, in which data for different fields is interleaved. This is often more intuitive, and supported directly by most programming languages.

Text formatting is by me

So, lets look at your example. First lets simplify, we have a patient right:

Patient
  - name
  - age
  - id

Lets just consider these fields for now. We can easily make a struct to represent this:

struct Patient {
   std::string name;  // std::string is for c++ and much easier to use than a char array
   unsigned age;
   int id;
};

Now we can have an array of patients as you have done, like this:

Patient patients[NUM_PATIENTS] = {{"bob", 20, 1}, ...};

But this sucks, what if we want to add a new patient? We can't, we have to know in advance how many we have. So c++ has the solution! std::vector:

std::vector<Patient> patients = {{"bob", 20, 1}, ...};
patients.push_back(Patient{"ann", 35, 52});

Now to sort them is very easy! c++ gives us std::sort! All we need is a way to compare. Lets take age for example:

auto age_compare = [](const Patient& lhs, const Patient& rhs) { 
    return lhs.age < rhs.age;
}

This is a lambda. A special little inline function that you can use. For example, image we have bob and ann from the above example. Bob is 20, ann is 35, so we can do:

age_compare(bob, ann); // This will be true because bob is younger than ann. 

With that we can sort our vector!

std::sort(std::begin(patients), std::end(patients), age_compare);

How easy is that!

Upvotes: 2

Related Questions