You're trying to parse a char array instead of a string. Either change string &letter to char letter[] and letter.size() to sizeof(letter) or change name's typename to string.
To add to that, what is happening is when main calls SetLetterUpper, it creates a temporary std::string object from name because SetLetterUpper requires a string, and there is a 1-argument std::string constructor that takes a char*. This temporary value is passed to the function and manipulated. When the function returns, the original char* remains untouched, and that is what you are printing out.
@Stremik
If we're talking about cstring (char pointers) functions, almost all of them are made unnecessary if we're using C++'s std::string class instead.
C++ itself does not have == operator specifically for comparing char pointers in a logical way (it only compares the actual memory addresses).
std::string == operator does have operator overloading, to allow comparing an std::string to a char pointer or string literal, but does not have operator overloads for comparing std::string to character (char, not char*).
Ex:
1 2 3 4 5 6
#include <string>
int main() {
std::string a = "h";
char b = 'h';
if (a == b) {}; // would not compile
}
BTW It seems to me that the reason for which std::string == operator does not
have operator overloads for comparing std::string to character might be some
heredity that came from C or even something that has its roots in architecture
of a computer. Maybe the way memory is accessed?
Can you elaborate on this caveat.
I don't know why std::string doesn't have == overloads for char (or for that matter cannot be constructed from a char), it might just have been a conscious decision by the library writers to do so, but I don't see why it would cause problems, maybe someone else knows more.
You saying do this?
1 2 3 4 5
std::string a = "h";
char c = 'h';
char* b = &c;
(a == b);
It would compile, but smells like undefined behavior because char c isn't necessarily going to be null terminated.
You right!. There could be anything in memory right after that char variable.
Dang! I never thought that transitioning into C++ would actually make me go back
and think in C, thus reinforcing it in my memory even more. I like it!
I was sad about having to give up C.
void SetLetterUpper( char letter[30] , char character)
{
for (int i = 0; i < strlen(letter) ; i++)
if ((letter[i] == character)&&(islower(letter[i])))
/*update*/ letter[i]=toupper(letter[i]);
}
This should work. Since you're working with character manipulation, I strongly suggest you use character arrays instead of strings, they're much easier to work with in this context.
@ne555 I guess, its more portable, right? I mean, since some people, well atleast where I'm from, use Turbo C++, and strings aren't really support out of the box..
( ps - I'm an avid Code::Blocks user and completely against Turbo, but I don't even know why, but schools here have this unreasonable love for Turbo, and thus, along with them, all/most of the students..)