Ah, the curly braces are in the format string.
Typically, format specifiers are of the form "%f" -- a percent sign delineates the format specifier.
In your case, the format specifier is delineated by curly braces: "{0}".
The tricky part is that your function can do something like this:
write( "swapped: {1}, {0}\n", 7, 9 );
and get
The very first thing you should do is scan your
format string to see
how many arguments there are. In the example I just gave, the largest number in {curly braces} is 1, so there are two double arguments.
Next, since you are using C++, and you know all the arguments are (supposed to be) doubles, create a std::vector and get all the arguments into that vector. (This is where you'll use the va_list:
1 2 3 4 5 6
|
std::vector<double> args( number_of_args );
va_list vargs;
va_start( vargs, format );
for (auto& arg: args)
arg = va_arg( vargs, double );
va_end( vargs );
|
Now you have an
indexable list of the argument values.
The last thing to do is scan through your
format string (again), printing every character that isn't in {curly braces}. So:
write( "I have {0:i} hamburgers at {1:c} each.\n", 4, 2.99 );
would print in pieces:
"I have " (printed character-by-character)
"4" (formatted)
" hamburgers at " (printed character-by-character)
"$", "2.99" (formatted)
" each.\n" (printed character-by-character)
The code to print formatted stuff is not too difficult. For example, for the fourth part (currency):
std::cout << "$" << std::fixed << std::setprecision( 2 ) << args[ n ];
You'll need a switch statement to choose from among the specifiers ('c', 'e', 'f', and 'i').
Don't worry about the extra credit until you get everything else working. Once you do that, the extra credit is pretty easy to add, but until then, don't.
Hope this helps.