0

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.

  • Is this an academic exercise to re-invent things or just to get the job done? `std::vector` and `std::sort` are the way to go here. – tadman Nov 25 '20 at 04:00
  • Tip: Give your functions one job and **one job only**. This "print" function does way too much. It takes input. It sorts. It presents. Those are three functions. – tadman Nov 25 '20 at 04:01
  • 1
    Ah, I'm still pretty new so I just looked up a bubble sorting algorithm. I'll look into sort. Thanks for the input! – Davin Duran Nov 25 '20 at 04:01
  • Don't just smash in search terms, have a solid reference to turn to first like [this one](https://en.cppreference.com/w/). C++ can be a bit intimidating, but at least take a quick tour to see what tools are on hand before you go and (painfully) re-invent them. "Bubble Sort" is a notoriously awful sorting algorithm and is only really useful as an example of how not to do it, so once you've built one, if you're looking to learn more about sorting, [explore better options](https://en.wikipedia.org/wiki/Sorting_algorithm). Many of these are deceptively simple but perform *way* better. – tadman Nov 25 '20 at 04:02
  • Gotcha, my instructor specified the parameters and purpose of the function so unfortunately that can't be helped. I'll keep that in mind for the future though. – Davin Duran Nov 25 '20 at 04:03
  • You may also want a [book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) you can turn to for advice on how to solve problems. Many of these cover not only the practical details, but how to approach the problem strategically. – tadman Nov 25 '20 at 04:04
  • 1
    Ok, I'll stay away from bubble sort in the future. Thanks again! – Davin Duran Nov 25 '20 at 04:04
  • If this is for a course, be advised that the vast majority of C++ courses are less for teaching you how to write C++ than they seem to be to inflict misery and punishment on students to act as a sort of filter. Hope you survive it! – tadman Nov 25 '20 at 04:05
  • You don't need to manually close the file. It will be closed when the stream goes out of scope and is destroyed. Doesn't hurt anything to do it manually but saves you some typing not to. – Retired Ninja Nov 25 '20 at 04:08

0 Answers0