[snip]
The fact that it doesn't should ring alarm bells. Strictly speaking, the
overloads for operator<< for the basic integral types are declared in
<ostream>. I'd be inclined to try including that header before patching
the problem with casts.
The above suggestion is excellent and I learned some more digging
around in it's implementation.
I really like the idea of getting away from the undesirable cast and
the impact it has on the readability of the program; however, the
problem did not disappear as hoped.
=====================================
#include <string>
#include <iostream>
#include <ostream>
void main( )
{
using namespace std;
cout << "the program is now paused ... ";
cout << "any erroneous typing will be discarded" << endl;
cout << "type away if you like and ";
cout << "hit return when ready to continue." << endl;
std::string str;
std::getline(cin,str);
cout << "str: " << str << endl;
cout << "str size: " << static_cast<int>(str.size());
cout << "\n" << flush;
}
=====================================
The addition of:
#include <ostream>
Still generates the warning of a size_t to int for the str.size()
function.
The C++ style:
static_cast<int>(str.size())
works without any warnings.
I see <ostream> defines "flush". Earlier I had problems using "flush"
and resorted to using "endl". Now flush works as advertised. An F1
help with ostream selected shows several Str and char type overloads
for operator<<. No overloads to be found for types: int, double,
size_t, etc. within <ostream>.
My guess is another header is needed and then the cast can disappear?
This newbie hasn't yet found the right header.
====================================
I appreciate the earlier suggestion to use:
int main()
And the kind reference to the electronic FAQs that deals with this
specific issue. I dug around for my "read somewhere" memory and found
it in none other than the Cline/Lomow original text on p.#5, FAQ #10
which states:
"We allow the compiler to supply an implicit int return type for
main(); all other functions have an explicit return type. Also, we
exploit the new C++ feature that requires the compiler to insert an
implicit return 0; at end of main()."
The examples within the original text use:
main() // without int return type.
I need to conform wherever possible for best communication and am
confused now on which is the best practice on such a simple issue. The
MS examples are also without the int return type. The electronic FAQs
is more dated, but more available to the masses.
Perhaps those using various compilers can advise if there is a
portability issue?
Maybe it is a case where both are equally right? They do both work on
the compiler I am using. But lean, clean, and fresh is sure a nice
thing.
-- Tom