I'm trying to use printf to print an item in a vector<string>. What do I use?
I assumed it'd be along the lines of printf("%s \n", vector[0]); but this causes it to crash. printf("%s \n", &vector[0]); prints among other gibberish, love hearts which while heart warming isn't all that useful.
%s wants a C-string (char*). You can use the .c_str() method of strings to get a c-string out of it. However, why exactly do you need to use printf() when you can just use cout? You are already using C++.
To answer your qestuion it's a point of curiosity as well as it looking more compact/neater.
For example:
cout << "Incorrect number of parameters for clause \"" + vectorToCheck[0].substr(0, vectorToCheck[0].find_first_of(",")) +"\" on line: " + lineToCheck.substr(0, lineToCheck.find_first_of(",")) << endl;
to me seems less readable than:
printf("Incorrect number of parameters for clause \"%s\" on line: %s.", vectorToCheck[0].substr(0, vectorToCheck[0].find_first_of(",")), lineToCheck.substr(0, lineToCheck.find_first_of(",")) );
It crashes if you do anything wrong instead of giving compile errors.
It can be used easily wrong and hence is the source for a lot of printf-exploits (Exploits are bugs that can be used to break into computer systems).
It increases code bloat. Also, even one printf in your code make you link against a lot of code, as the compiler can't know which part of the big printf-implementation you are using. (This is not really interesting for C++. iostream is not that small either. Even if you only link against what you use, often it tend to be larger than the whole C-printf implementation in the end. But even in C, a lot of people shun "printf").
cout << "#STAT reports the wrong amount of source lines, there are actually: " + noOfSourceLines << endl;
With noOfSourceLines being an integer and it prints:
"reports the wrong amount of source lines, there are actually: "
Hence changing it to...
stringstream ss;
ss.str("");
ss << noOfSourceLines;
cout << "#STAT reports the wrong amount of source lines, there are actually: " + ss.str() << endl;
EDIT: The ss.str("") may appear superfluous here but it's to clear it - ss is defined elsewhere and used multiple times in the actual code.