Here is my code:
void printSortedData(Car garage[], int garageArrsize, string output, string brands[], string colors[], string models[], int otherArrsize)
{
ofstream outputFile(output);
char choice = '\0';
cout << "\n\nHow would you like the data sorted?";
cout << "\n1. Brands";
cout << "\n2. Models";
cout << "\n3. Colors\n";
cin >> choice;
if (choice == 1)
{
for (int count1 = 0; count1 < garageArrsize; count1++)
{
for (int count2 = 0; count2 < garageArrsize - count1; count2++)
{
if (garage[count2].brand > garage[count2 + 1].brand)
swap(garage[count2], garage[count2 + 1]);
}
}
for (int count = 0; count < garageArrsize; count++)
{
outputFile << "Car #" << count + 1 << "\n";
outputFile << "---------------------\n";
outputFile << garage[count].brand << endl;
outputFile << garage[count].model << endl;
outputFile << garage[count].color << endl;
outputFile << garage[count].speed << endl;
outputFile << "---------------------\n";
outputFile << " ";
}
}
So the idea is to sort an array of structures based on user input. I omitted the if else statements for choices 2 and 3 since they're essentially the same. My issue is, whenever I go to view the output file after running the program, it's completely blank. However, I have a separate function which also outputs to a file without sorting, and that one works fine. The code for that:
void printUnsortedData(Car garage[], int garageArrsize, string output)
{
ofstream outputFile(output);
for (int count = 0; count < garageArrsize; count++)
{
outputFile << "Car #" << count + 1 << "\n";
outputFile << "---------------------\n";
outputFile << garage[count].brand << endl;
outputFile << garage[count].model << endl;
outputFile << garage[count].color << endl;
outputFile << garage[count].speed << endl;
outputFile << "---------------------\n";
outputFile << " ";
}
outputFile.close();
}
Since it's relevant, I do make sure to close the output file for the sorted function at the end. Do you guys know what the issue is?
Update: I've since updated the code some so that the sorting works a bit different:
void printSortedData(Car garage[], int garageArrsize, string output, string brands[], string colors[], string models[], int otherArrsize)
{
ofstream outputFile(output);
char choice = '\0';
cout << "\n\nHow would you like the data sorted?";
cout << "\n1. Brands";
cout << "\n2. Models";
cout << "\n3. Colors\n";
cin >> choice;
if (choice == 1)
{
sort(garage, garage + garageArrsize, brandSort);
for (int count = 0; count < garageArrsize; count++)
{
outputFile << "Car #" << count + 1 << "\n";
outputFile << "---------------------\n";
outputFile << garage[count].brand << endl;
outputFile << garage[count].model << endl;
outputFile << garage[count].color << endl;
outputFile << garage[count].speed << endl;
outputFile << "---------------------\n";
outputFile << " ";
}
}
However, the file still displays nothing.