'Trouble printing a sorted array while counting and printing how often it occurs

First semester Comp Sci 1 student, working on an assignment and while its practically all finished, it prints some items more than once based on how often it occurs rather than once. For example: 1 load of radishes, 2 loads of yams, 2 loads of yams, 1 load of beets, etc. I'm sure it is due to how I structured my loops but I cannot think of the logic or code that would check for uniqueness but only print once while still counting the loads and displaying the amount of loads. Any advice or tips would be great.

#include <iostream> //directives to use the libraries I need
#include <fstream>
#include <iomanip>
#include <limits>
#include <string>

using std::cin; //my std calls in order to avoid namespace std
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::left;
using std::setw;

void createHeader(int spaces); //my function prototypes and headers
void reportNsort(ifstream &input);

int main()
{
   ifstream input;

   input.open("ASSGN6-A.txt"); //opens the file and checks that it opened
   if (!input)
   {
       cout << "The file failed to open!" << endl;
   }

   createHeader(65); //call to my functions
   reportNsort(input);

   return 0;
}

void createHeader(int spaces) //prints out the header
{
     for (int i = 0; i < spaces; i++)
   {
       cout << "=";
   }
   cout << endl;
   cout << "= " << "                 " << "FARMER'S MARKET INVENTORY " << "                   =" << endl;
   for (int j = 0; j < spaces; j++)
   {
       cout << "=";
   }
   cout << endl;
}

void reportNsort(ifstream &input) //runs the various sorts and prints the inventory to the screen
{
    char farm[25]; //all the variables I need
    const int ArrSize = 28;
    string item[ArrSize];
    string temp;
    int count = 0;
    int numItems;
    int numLoads;
    double pricePer, totalItems = 0;

    input.getline(farm,25,','); //reads in first set of data to make sure the standard input.eof reads correctly
    input >> numItems;
    input >> item[count]; //reads the data from the file into the array I will sort later
    count++;
    input >> pricePer;
    totalItems += numItems;

    while(!(input.eof()))
    {
        input.getline(farm, 25, ','); //continues to take data from the input file
        input >> numItems;
        input >> item[count];
        count++;
        input >> pricePer;
        totalItems += numItems;
    }

    input.close();

     for (int i = 0; i<(ArrSize-1); i++) //sorts the data in the array into the alphabetized list
    {
        for(int j = 0; j<(ArrSize-i-1); j++)
        {
            if(item[j]>item[j+1])
            {
                temp = item[j];
                item[j] = item[j+1];
                item[j+1] = temp;
            }
        }
    }

    for (int k = 0; k < ArrSize; k++) //compares the items and adds them together based on how many of each type of item was delivered
    {
        numLoads = 0;
        for (int l = 0; l < ArrSize; l++)
        {
            if (item [l] == item [k])
            {
                numLoads++;

            }
        }
        cout << left << setw(2) << numLoads << "loads of " << item[k] << " were delivered" << endl; //was unable to stop it from printing out some things more than once if it was the same load!
    }

    cout << "There were " << totalItems << " items contributed to this week's event." << endl;
}


Solution 1:[1]

Think about your array and the way you are counting how many times each element is present.

Let's say you have an array composed of [radishes, yam, beets, yam], after filling it you sort it alphabetically so it becomes:

[beets, radishes, yam, yam]

Then you are outputting how many times a single entry occurs. But this means that yam will be outputted twice, once for the first entry and once for the second.

If you want to print it only once you need to skip all the other occurrences of a certain entry, fortunately your array is ordered so you can be sure that all the values that are the same will be one right after the other.

You could change your outer for loop to go "forward" by the number of elements you just counted instead of doing k++. Maybe something like:

int numLoads = 0;
for (int k = 0; k < ArrSize; k+=numLoads) // go forward by how many items you've found
{
    numLoads = 0; // reset the count for every item
    for (int l = 0; l < ArrSize; l++)
    {
        if (item[l] == item[k])
        {
            numLoads++;

        }
    }
    cout << left << setw(2) << numLoads << "loads of " << item[k] << " were delivered" << endl; //was unable to stop it from printing out some things more than once if it was the same load!
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Doe