C++: Mixing cout with sprintf and strings
char buffer[81]; // +sprintf char[] buffer.+ *How is a programmer meant to post code fragments to this forum when brackets, pluses, underscores, and asterisks are interpreted as markup?????*+
string s = "%-" + itos(longest) + "s "; // +itos() converts int to string. it works correctly.+
char * c = s.c_str(); // +convert format string to a char*, which sprintf requires+
* ERROR: invalid conversion from "const char *" to "char *" *
sprintf(buffer, c, (n < wordvec.size()) ? wordvec[n] : ""); //
* WARNING: cannot pass objects of non-POD type ... call will abort at runtime
cout << buffer;
---
.
Can anyone advise me on what is the right way to do this, without having to learn iostream manipulators and other grossness? (s)printf is SO easy....
Thanks,
Chap
2.4GHz MacBook Pro, Mac OS X (10.5)