updated c++ solution for problem #6