0

I am trying to make a program which will take in a list from the user with both quantities and words. However, I have everything done but am unable to combine the numbers with according word(s) on the same line. It continues to output all the numbers and then output all the words. Also the words should be outputted in alphabetical order. Thank you in advance!

{

set<string> sortedItems;
cout <<  " (type \"exit\" to exit, now press enter to begin listing): ";

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 take #'s


if (itemChoice == "exit")
{
    break;
}
sortedItems.insert(qtyChoice);
sortedItems.insert (itemChoice);

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

}

Instead of outputting the number of each and name of each on the same line, it does this:

1. 23 hello
2. 18 thanks
3. 37 goodbye
4. exit
exit
18
23
37
goodbye
hello
thanks
Hannah B.
  • 23
  • 3
  • 1
    What are the two initial `getline` calls for? And why read a quantity as a string? And why add it into the "***item***" set? Perhaps you should read more about classes or structures? – Some programmer dude Jan 14 '19 at 06:55
  • @Someprogrammerdude The get line is to read in the entire line of the user's input so that if they want more than one word. As for a string, that is to allow the user to input as many items as they want and still be able to alphabetize them. – Hannah B. Jan 14 '19 at 06:59
  • Alternative solution: `std::map`. The key is the item name, the data is the (*accumulated!*) quantity. – Some programmer dude Jan 14 '19 at 07:12
  • What order do you want thing to be printed in? You seem to imply that alphabetical order is wrong. – john Jan 14 '19 at 07:14
  • @john Yes, I want it organized in alphabetical order. – Hannah B. Jan 14 '19 at 07:17

1 Answers1

0

You need to structure your data. At the moment you are treating words and quantities exactly the same and so they print in a single list with the numbers before the words because alphabetically numbers come before words.

First create a structure for your data

struct LineItem
{
    int quantity;
    string choice;
};

Now unlike strings the compiler has no idea how to sort LineItems, you must tell it by supplying an operator< for LineItems. operator< should return true if the first LineItem is 'less than' the second LineItem. I'm assuming (perhaps wrongly) that you want to print in alphabetical order of the choices. So that would be

bool operator<(const LineItem& x, const LineItem& y)
{
    return x.choice < y.choice;
}

Then declare your set to be a set of LineItems

set<LineItem> sortedItems;

Then rewrite your input loop

for (int i = 1; ; i++)
{
    LineItem li;
    cout << i << ". ";
    cin >> li.quantity;
    cin >> li.choice;
    if (li.choice == "exit")
        break;
    sortedItems.insert(li);
}

Something like that anyway. You'll have other changes to make, for instance your print routine will have to change to use LineItems instead of strings, but I'll leave that to you.

The main is point is that by creating a LineItem struct you are grouping associated items of data (the quantity and the choice) so then you can print them together.

john
  • 85,011
  • 4
  • 57
  • 81
  • thanks!! but is there a way to do this without using Lineitem and sticking only to strings or maybe even arrays? – Hannah B. Jan 14 '19 at 07:26
  • @HannahB.I don't get it, what do you have against using a struct? Is there something about this task you haven't told us? I'm disappointed I went to the effort of writing the above answer if that's the case. Using structs (or classes) to group associated things is basic C++ programming. – john Jan 14 '19 at 07:28
  • Sorry, I would have gladly used your code for my answer but I am at a beginners- level and cannot include these things that we haven't learned yet. Thanks again. – Hannah B. Jan 14 '19 at 07:39
  • @HannahB. Sounds like a really bad course if you get penalised for showing initiative. Hard to answer because I don't know what you know and what you don't. Some programmer dude's comment above about using `std::map` is possible, apart from that I can't think of any simple technique. What can I say, using a struct is the right answer, and its a shame you can't use it. – john Jan 14 '19 at 07:59
  • @HannahB. The way of doing it without structs or maps is to have three parallel arrays. First array is the choice, second array is the quantity. The third array is an *index*. If starts off as {0,1,2,3,4,...}. Then you sort the index array (leave the other two alone) based on the value of the choice array at the given index. It's rather a complicated technique to expect a beginner to figure out. – john Jan 14 '19 at 08:03