   C++ Primer questions
  I'm working through the C++ Primer (4th ed.) and am having a lot of fun learning.  Along the way I've encountered a couple of exercises that I am having trouble solving due to difficulties interpreting the intention of the problem.  I was hoping that there are some of you on this site who could help me out, since the book was recommended in the beginner's section here.Exercise 1.24 (p.26) reads:Write a program that reads several transactions.  For each new transaction that you read, determine if it is the same ISBN as the previous transaction, keeping a count of how many transactions there are for each ISBN.  Test the program by giving multiple transactions.  These transactions should represent multiple ISBNs but the records for each ISBN should be grouped together.In the preceding exercises I've learned how to use the for, while, and if functions, as well as a the particular while function [ while (std::cin >> value) ] that operates as long as there is input.  We are also using the Sales_item class (of the format ISBN#, # of copies sold, avg. sales price) that allows for addition, assignment, and v1.same_isbn(v2) operations.The thing I am stumped about is how to group the different ISBNs together when I don't know the number of different ISBNs that the user will input.  All of the other examples either only accept one particular ISBN for the user, or simply print the total for the current ISBN and then switch to a running total of the new ISBN every time the user's entry has a different ISBN.  It sounds like this exercise wants me to file each entry into a different 'Sales_item total' variable for each unique ISBN.  I have no idea how to do this.Can anyone dispel my confusion?   
  I would say that the best thing to use is std::map. Have the key to the map as the ISBN number and the data referenced as information about the ISBN.Dave   ;  Note that it says "the previous transaction", not "a previous transaction." By "grouped together", it means that you don't have to worry about the user entering 12345, 66666 and then 12345 again. You only need to count when the data is like 12345, 12345, 12345, 66666, 66666, 99384, 99384, 99384 ...Quote:It sounds like this exercise wants me to file each entry into a different 'Sales_item total' variable for each unique ISBN. I have no idea how to do this.Have you seen the std::vector class yet?   ;  Dave:  I'm sure there are easy ways to do this, but all I have at my disposal (for now) are the few functions and operations I mentioned.  Hopefully I'll get to learn about std::map pretty soon; what exactly does it do?Fruny:  I think you hit the nail on the head.  I guess I was making the exercise far more complicated than it really is.  It looks like all I need to do is set up a while loop that contains an if statement checking the ISBN of each entry against the previous one.  If it matches I add it to the total, if not, I print whatever was already stored then flush out the total and start a new one with the new ISBN entry.I haven't seen the std::vector class yet.  I think I'm supposed to learn about it in chapter 4.Thanks for the help.  I'm so glad I found this site as a resource.  I think I might pick up the C++ in 21 days book for the forum course and join in on that as well.   ;Quote:Original post by industrialplaidHopefully I'll get to learn about std::map pretty soon; what exactly does it do?It is an associative container. Very roughly speaking, it works like an array (or a vector), except that its indices can be of any (ordered) type, and do not have to be consecutive. You could thus associate a counter with each ISBN, without having to have a huge array keeping trick of the counts for all possible ISBN.Quote:Fruny:  I think you hit the nail on the head.  I guess I was making the exercise far more complicated than it really is.  It looks like all I need to do is set up a while loop that contains an if statement checking the ISBN of each entry against the previous one.  If it matches I add it to the total, if not, I print whatever was already stored then flush out the total and start a new one with the new ISBN entry.Sounds good to me.Quote:I haven't seen the std::vector class yet.  I think I'm supposed to learn about it in chapter 4.With a vector, you could delay flushing out and printing the total until you are done entering the data. Consider that as an extra-credit assignment. [smile]Quote:Thanks for the help.  I'm so glad I found this site as a resource.  I think I might pick up the C++ in 21 days book for the forum course and join in on that as well.You're welcome.   