You are on the right track!
However, the
printf()
function only takes one format-string (with the '%' characters in it).
So things like this:
cout << "some text" << x << "some more text" << y;
Have to either (1) have their format strings all merged together; (2) have a "master format string" placed at the beginning; or (3) have to be broken into multiple calls to
printf().
Consider this:
int x;
float y;
cout << "some text" << x << "some more text" << y;
printf("some text %d some more text %f", x, y);
In this example, I took the strings, merged in the "%d" and "%f" place-holders for the variables, and produced one big format string. This is how most C programmers would do it, when writing the code from scratch.
Now consider:
cout << "some text" << x << "some more text" << y;
printf("%s %d %s %f", "some text", x, "some more text", y);
In this example, I left everything alone and just put a "master format" string at the front of the arg list. This is how I would do a conversion like you are doing. It's quick, and you don't have to shuffle things around. (In fact, I'd probably write a program to do it for me ;-).
Finally, consider:
cout << "some text" << x << "some more text" << y;
printf("some text" " %d", x); printf("some more text" " %f", y);
In this example, I added the format strings just in front of each variable, because when two strings are next to each other in C, like "foo" "bar" the C compiler will automatically merge them (this is only true for literal strings, not for string variables -- it happens at compile time, not run-time).
Next, I inserted
); printf(
after every variable. Then I cleaned up the end.
The result is that each variable has (maybe: a text string, and) a format string in front of it. This is another way that you could mechanically convert to printf.
The thing is,
any one of these will work! You just need to pick an approach you like.