OK, don't worry, you'll be able to cover all the material step by step.
Reading your code it seems that I was right, you can learn something
from writing such a simple function, before diving into more advanced
subjects - I'm sure you'll take my notes in the right way, I'm starting
to dive into C myself and I feel just fine knowing that I _don't know_
and getting advices.
I'll snip all of your code, I think you know your stuff in C, you only
need to straighten out some concepts to learn the C++ way, hence I'll
post here some sparse comments.
Don't use C-style casts, and even less use them for doing simple
conversions: use "long(value)" instead of "(long)value", and chances are
that you don't even need them in this case. More about this later.
Decide the exact behaviour expected by your code: as it is now it
truncates decimals instead of rounding them upon the amount requested by
the caller.
You're hardcoding the separators and their frequency by setting just two
styles via one flag, you could make it more flexible by passing those
separators and frequency and so on as separate parameters, and with some
struggle you could give some memory to that function using static
locals, but I'd suggest you to start at once by creating a class.
A skeleton you can adapt to your task:
//-------
#include <iostream>
#include <sstream>
class NumberFormatter {
public:
NumberFormatter(int decimals = 2,
char separator = '.' )
: decimals(decimals),
separator(separator) {}
std::string operator()(double value) const;
private:
int decimals;
char separator;
};
std::string NumberFormatter:
perator()(double value) const {
std::stringstream result;
// intentionally silly example
result << long(value) << separator << decimals;
return result.str();
}
using namespace std;
int main() {
NumberFormatter f1;
NumberFormatter f2(3, '-');
cout << f1(4.2) << endl;
cout << f2(7.8) << endl;
return 0;
}
//-------
There are other things you can improve about your approach and I'll
eventually come back on them if others don't point them out.
Give it a shot with the above skeleton, that's an outline of what is
commonly called a function-object or functor.
Don't forget that a good book and the FAQ are two very powerful tools
for your C++ learning and for getting the best help out from this group,
start by reading sections 5 and 6 of the FAQ to ensure yourself on the
best track.
Have fun with C++, on my part I'm trying to have fun with C ;-)