0

I'm stuck on trying to add quantities to this list when it returns the output. It is essentially a list entered by the user that starts with a quantity and is followed by a word. It is then supposed to output the same list but in alphabetical order. I currently have it alphabetized but the numbers that the user enters on the same line, are returned on a separate line from the word when returned in alphabetical order. I know I am prompted to use parallel arrays but have no clue how to incorporate them into this. Nobody has been able to reply to me but I know it is do-able. Thank you all in advance!!

#include <iostream>
#include <string>
#include <iomanip>
#include "functions.h"
#include <algorithm>
#include <set>
using namespace std;


void print(const string& item)
{
    cout << item << endl;
}


int main(void)
{
    const int MAX_LENGTH = 256;
    string items [MAX_LENGTH];
    int quantities [ MAX_LENGTH];
    string itemChoice;
    string qtyChoice;
    int numItems= 0;

    int randomArray[MAX_SIZE];  

    {
        set<string> sortedItems;

        cout <<  " (type \"exit\" twice to exit, now press enter twice to begin listing your shopping list.): ";

        getline (cin, qtyChoice);
        getline(cin, itemChoice);

        for (int i = 1; ; i++)
        {
            string itemChoice;
            string wholeOrder;
            cout << i << ". ";
            cin >> itemChoice;
            cin >> qtyChoice; // this is how I got it to intake #'s
            //getline (cin, qtyChoice);// putting these here actually allow both items to be on one line!! but it leaves awkward spaces.
            //getline(cin, itemChoice);
            //getline (cin, qtyChoice);

            if (itemChoice == "exit")
            {
                break;
            }

            sortedItems.insert(qtyChoice);
            sortedItems.insert (itemChoice);
            //sortedItems.insert(itemChoice);   
        }


        for_each(sortedItems.begin(), sortedItems.end(), &print);


        return 0;
    }

this is my code and this is what happens as output

(type "exit"to exit, now press enter twice to begin listing your list.):

1. 3828 eijsd
2. 38238 shd
3. 382 hsdid
4. exit
382
38238
3828
eijsd
hsdid
shd
JaMiT
  • 14,422
  • 4
  • 15
  • 31
Hannah B.
  • 23
  • 3

1 Answers1

0

You make no distinction between quantities and words in your set, so they are treated as the same kind of thing, with no connections between them. To preserve the connection, you need a more complicated data structure.

Using parallel arrays is one possibility, but the overhead is a pain. A simpler approach than a pair of arrays would be an array of pairs -- better yet a set of pairs, as you already know that sets provide easy sorting. (A map would also work.)

set< pair<string,string> > sortedItems;

Storing both the word and the quantity in the pair retains the relationship between them. I'll leave it to you to fill in the rest of the details.

JaMiT
  • 14,422
  • 4
  • 15
  • 31