Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > error trying to toupper string

Reply
Thread Tools

error trying to toupper string

 
 
sandy@murdocks.on.ca
Guest
Posts: n/a
 
      11-29-2006
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.

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      11-29-2006
<> wrote in message
news: oups.com...
>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


 
Reply With Quote
 
 
 
 
BobR
Guest
Posts: n/a
 
      11-29-2006

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:stream &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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
toupper UTF8 string David RF C Programming 9 09-27-2009 05:49 AM
toUpper and pointer to a string gaga C++ 16 09-23-2007 08:28 PM
error trying to toupper string sandy@murdocks.on.ca C++ 4 11-30-2006 01:00 AM
toupper wont work ben C++ 5 02-25-2004 09:54 AM
String.ToUpper Kerri ASP .Net 2 10-27-2003 12:09 AM



Advertisments