What are some good exercises for an experienced programmer new to C++? I have found lists and suggestions on the web, but they are always simple or I have already completed them in other programming languages like C or Basic. Something that will help with learning modern C++ is best.

First tell me what do you know about C++? And check out my sig.

Most C++ books that you buy will have end-of-chapter tests or reviews that have a lot of problems for you to solve. You can get some books from your library or find some e-books and try the problems they give at the end of their chapters.

This is my solution to your signature problem. Does it tell you what I know about C++?

``````#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

string toMathEquation(string const& expr);
bool is_operator(string const& term, string& val);
bool is_degree(string const& term, string& val);
bool is_literal(string const& term, string& val);
bool is_match(pair<string, string> items[], size_t sz, string const& term, string& val);
string to_string(size_t val);

int main() {
cout<< toMathEquation("x squared plus nine x minus seven") <<endl;
cout<< toMathEquation("two plus six x cubed minus x squared") <<endl;
cout<< toMathEquation("negative x cubed divided two x squared") <<endl;
cout<< toMathEquation("twenty-five times negative six x quartic") <<endl;
}

string toMathEquation(string const& expr) {
string term, equation, val;
if (is_operator(term, val) || is_degree(term, val) || is_literal(term, val))
equation += val;
else
equation += term;
}
return equation;
}

bool is_operator(string const& term, string& val) {
pair<string, string> ops[] = {
make_pair("plus", " + "), make_pair("minus", " - "),
make_pair("times", " * "), make_pair("divided", " / "),
make_pair("negative", "-")
};
return is_match(ops, sizeof ops / sizeof ops[0], term, val);
}

bool is_degree(string const& term, string& val) {
pair<string, string> deg[] = {
make_pair("squared", "^2"),
make_pair("cubed", "^3"),
make_pair("quartic", "^4")
};
return is_match(deg, sizeof deg / sizeof deg[0], term, val);
}

bool is_literal(string const& term, string& val) {
string words[] = {
"one","two","three","four","five",
"six","seven","eight","nine",
"ten","eleven","twelve","thirteen","fourteen","fifteen",
"sixteen","seventeen","eighteen","nineteen",
"twenty","thirty","forty","fifty",
"sixty","seventy","eighty","ninety"
};
vector<pair<string, string>> literals;
for (size_t x = 1, k = 0; x < 100; /* see body */) {
if (x < 20)
literals.push_back(make_pair(words[k++], to_string(x++)));
else if (x % 10 == 0) {
literals.push_back(make_pair(words[k], to_string(x++)));
for (size_t y = 0; y < 9; ++y)
literals.push_back(make_pair(words[k] + "-" + words[y], to_string(x++)));
++k;
}
}
literals.push_back(make_pair("one-hundred", "100"));
return is_match(&literals[0], literals.size(), term, val);
}

bool is_match(pair<string, string> items[], size_t sz, string const& term, string& val) {
pair<string, string>* match = find_if(items, &items[sz],
[term](pair<string, string> p) { return p.first == term; });
bool is_match = match != &items[sz];
if (is_match)
val = match->second;
return is_match;
}

string to_string(size_t val) {
stringstream ss;
ss<< val;
return ss.str();
}``````

One common c++ exercise is: Write a recursive function that gathers a list of all the files and folders on the hard drive, starting with a folder specified by the user. You will need to maintain either a std::vector or std::list (your choice) of all the files, and the file names must contain the complete path. When finished, short the array or list alphabetially by calling std::sort and supplying it your own comparison function. Display al the files after the vector or list has been created. Note: Don't copy/paste code you might find on the net -- that's considered cheating.

Try to write a serialization library like this one. That should keep you busy for a while, and help you learn advanced OOP concepts of C++ that may challenge you a bit. Also see these tutorials, they will show many nice ways of mixing template meta-programming and generic programming, that you don't find as much in other languages.

But again, it's hard to tell how "experienced" you are, but you sure solved firstPerson's problem quickly.

>>Does it tell you what I know about C++?

Not really. Although you *solved the problem fast, it could be better. How far of C++
do you know? Any knowledge about meta-programming? Usually, the best way to get better at programming is to design project. That way you get a chance to practice algorithms and design patterns. But if you just want some problems to solve, then
google online. There are a lot of sites dedicated to practice problems.

-----
*solved: haven't really tested the program, just taking your word.

Although you *solved the problem fast, it could be better.

I would like to know how to make it better, but that is a question for another thread.

How far of C++ do you know? Any knowledge about meta-programming?

The C subset, classes and basic OO. I know about template for simple genericity and some STL, but no meta-programming.

Do you know how to use polymorphism? Virtual inheritance? Pure virtual functions?
Problem with inline virtual functions? Try projecteuler.net. They have challenging
math problems for practice. Also research design patterns. There are plenty of stuff to
learn. Here read this. That should keep you busy.
Also how learning to implement different data structures? List, trees, TRIE ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.