C2. Functions with variable number of arguments

Write a function write with variable number of arguments that takes a string first argument followed by any number of arguments of type double and prints on the screen a string formatted by the rules described below. The first argument may contain formats in curly braces of the form {index[:specifier]}, where the square brackets show optional parts (this is :specifier may be missing), and index is the sequence number of an argument of type double (starting from sequence number 0).
Rules for formatting: In the printed string the curly brackets and their content will be replaced by the argument with the given index, formatted according to the given format specifier. If the format specifier is missing, the argument will be printed with its default format. For example:
write("The number {0} is greater than {1}.", 5, -3);
will print
The number 5 is greater than -3.
write("There are no format specifiers here.");
will print
There are no format specifiers here.

The format specifiers and their meanings are listed in the following table

Specifier Meaning Format Output for 1.62 Output for 2.0
none default {0} 1.62 2
c currency {0:c} $1.62 $2.00
e scientific {0:e} 1.620000e+000 2.000000e+000
f fixed point {0:f} 1.620000 2.000000
i round to int {0:i} 2 2
Note: an overview of the C++ ios flags is available at http://www.cplusplus.com/reference/ios/ios_base/fmtflags/
Limitations: You may limit the maximum number of arguments your function can process to a certain value, for example 10.
Topic archived. No new replies allowed.