Jim Langston wrote in message ...
>
This compiles and runs for me with the expected output of
HELLO
I'm using Micorost Visual C++ .net 2003
#include <iostream>
#include <string>
std::string toupper(std::string S){
for (int i=0; i < S.length(); ++i){
S[i]=toupper(S[i]);
}
return S;
}
int main(){
std::string MyString("Hello");
std::cout << toupper( MyString ) << "\n";
std::string wait;
std::getline( std::cin, wait );
} // function main
Hi Jim,
You should kick up the warning level a notch or two:
//for( int i=0; i < S.length(); ++i){
// [Warning] comparison between signed and unsigned integer expressions
// ....or is type 'int' unsigned on your machine? <G>
for( size_t i(0); i < S.size(); ++i){
// or 'std::size_t'. 'std::string::size_type' is worth a typedef (and I hate
typedef's!!<G>)
Otherwise, works fine on MinGW(GCC3.3.1).
Now, put 'toupper' (as is) in a class, and see if it still works as
advertised (call from inside the class).
class ToUpTest{ public:
void Print(std::string str, std::ostream &sos){
sos<<toupper(str)<<std::endl;
return;
} // Print(string,ostream&)
std::string toupper( std::string S ){
for( size_t i(0); i < S.size(); ++i ){
// S[ i ] = toupper( S[ i ] );
// In member function
//`std::string ToUpTest::toupper( [snip] )':
// error: invalid conversion from `char' to `const char*'
// error: initializing argument 1 of [snip]
// error: cannot convert `std::string' to `char' in assignment
S[ i ] = std::toupper( S[ i ] ); // even '::toupper()'
}
return S;
} // toupper(string)
};
{ // main() or function
std::string MyString("Hello");
// std::cout << toupper( MyString ) << "\n";
ToUpTest TuT;
TuT.Print( MyString, std::cout );
}
See the OPs problem now?
--
Bob R
POVrookie