Connecting Tech Pros Worldwide Forums | Help | Site Map

error trying to toupper string

sandy@murdocks.on.ca
Guest
 
Posts: n/a
#1: Nov 29 '06
I am trying to upper case a string, so I have this method:

string FileSystem::toupper(string S)
{
for (int i=0; i<S.length(); ++i)
{
S[i]=toupper(S[i]);
}

return S;
}

I get the errors:
invalid conversion from `char' to `const char*'

`std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
_CharT*, const _Alloc&) [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]'

cannot convert `std::string' to `char' in assignment

I did some looking on the Net and it appears I have done nothing new,
in fact it is almost character by character a match of 3 samples I
found on the net.

But of course... it doesn't compile.


Jim Langston
Guest
 
Posts: n/a
#2: Nov 29 '06

re: error trying to toupper string


<sandy@murdocks.on.cawrote in message
news:1164766528.879075.280170@h54g2000cwb.googlegr oups.com...
Quote:
>I am trying to upper case a string, so I have this method:
>
string FileSystem::toupper(string S)
{
for (int i=0; i<S.length(); ++i)
{
S[i]=toupper(S[i]);
}
>
return S;
}
>
I get the errors:
invalid conversion from `char' to `const char*'
>
`std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
_CharT*, const _Alloc&) [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]'
>
cannot convert `std::string' to `char' in assignment
>
I did some looking on the Net and it appears I have done nothing new,
in fact it is almost character by character a match of 3 samples I
found on the net.
>
But of course... it doesn't compile.
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


BobR
Guest
 
Posts: n/a
#3: Nov 29 '06

re: error trying to toupper string



Jim Langston wrote in message ...
Quote:
>
>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


Closed Thread