Question

I searched a lot on google and stackoverflow but I couldn't find my answer. I'm actually reading a C++ book (C++ Primer 5th Edition) and they're asking me to do an exercise.

"Write a program that reads several transactions and counts how many transactions occur for each ISBN" (Console Project)

This is my code atm :

Sales_item currentItem, item;

if (cin >> currentItem)
{
    int cnt = 1;
    while (cin >> item)
    {
        if (currentItem.isbn() == item.isbn())
        {
            ++cnt;
        }
        else
        {
            cout << currentItem.isbn() << " occurs " << cnt << " times " << endl;
            cnt = 1;
            currentItem = item;
        }
    }
cout << item.isbn() << " occurs " << cnt << " times " << endl;
}

I won't explain how work the transactions so I'm gonna ask it in another way.

I type in my console 6(or more) random strings as exemple:

101A
102A
101A
101A
103A
102A

I want the result the result (output) to be:

101A occurs 3 times.
102A occurs 2 times.
103A occurs 1 times.

How would you do that?

Was it helpful?

Solution

Using std::map instead of list will be easier.

int main()
{
  map<string,int> stringMap;

  for (int i=0;i<3;i++)
  {
    cout<<"Enter string: ";
    string s;
    cin>>s;
    if(stringMap.find(s)!=stringMap.end())
    {
      stringMap[s]++;
    }
    else
    {
      stringMap[s]=1;
    }

  }
  for (map<string,int>::const_iterator itr = stringMap.cbegin();  itr!=stringMap.cend(); ++itr)
  {
    if(itr->second > 1)
       cout<<itr->first << " occurs "<<itr->second<<" times"<<endl;
    else
       cout<<itr->first << " occurs "<<itr->second<<" time"<<endl;
  }


  return 0;
}

OTHER TIPS

There are multiple approaches to a question like this, so the best would depend on your constraints. My approach would be:

while(GetInputString(str)) {
  myStruct* ptr = existingList.Find(str);
  if (!ptr) {
    existingList.Add(str);
  } else {
    ptr->IncrementCount();
  }
}

I've tried not to solve your problem for you - hopefully the answer gives you a template to work with...

    #include <iostream>
#include <vector>
#include "Sales_item.h"

using namespace std;

void book_transactions(string isbn, int count);
int main()
{
   vector<Sales_item> book_vec;

   Sales_item book;
   int total_transactions = 1;

   // Reads in Sales_item objects
   while (cin >> book)
   {
       book_vec.push_back(book);
   }

   // Compares ISBNs in vector, if two isbns are equal total transactions increases
   for (int i = 0; i < book_vec.size() - 1; i++)
   {
       if (book_vec[i].isbn() == book_vec[i+1].isbn())
       {
          total_transactions = book_vec[i].get_units_sold() + book_vec[i+1].get_units_sold();
          book_transactions(book_vec[i].isbn(), total_transactions);
       }

   }
}

void book_transactions(string isbn, int count)
{
    cout << "ISBN: " << isbn << " " << "Transactions: " << count << endl;
}
  • I went into the Sales_item.h class and added a method to get the number of units sold. There was already a class member (unsigned return type) under private. I just created a getter and then created a vector of Sales_items. This solution seems to work. I'm sure the intended problem is not for an N set of sales_items, however this should work as a "duct-tape" solution. There is no need to play around with pointers at this point. A simple vector array and an algorithm to compare adjacent objects works just fine.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top