Problems with str.replace function

This is a discussion on Problems with str.replace function within the C++ Programming forums, part of the General Programming Boards category; I can't get this function to compile. All it does is converts all characters one by one to upper case ...

  1. #1
    Unregistered
    Guest

    Problems with str.replace function

    I can't get this function to compile. All it does is converts all characters one by one to upper case characters. When I try to assign these upper case character to a new string (not allowed to use arrays) I have problems. I'm using VC++ 6.0 standard. A friend used DJGPP and had no problems.

    I keep getting one error message on the last line where replace
    is called:

    error C2664: 'class std::basic_string

    I'm guessing it has to do with assigning a character to a string variable but I haven't seen anything (in text books) that says you're not allowed to do that.

    Am I using this function incorrectly or is there a problem with the function in the string class?

    Heres the code:
    ------------------------------------------------------------------------------------
    // Accept mixed-case characters of input string and return string containing all upper-case characters

    string allcaps (string inputStr)
    {
    // Scroll through each character in the string
    for (int charpos = 0; charpos < inputStr.length(); charpos++)
    {
    // Assign current character to temp char variable
    char selectedChar = inputStr.at(charpos);

    // Re-Assign upper-case equivalent of selectedChar to itself
    selectedChar = toupper(selectedChar);
    inputStr.replace(charpos, 1, selectedChar);
    }

    return inputStr;
    }

    Thanks in advance!

  2. #2
    William
    Guest
    The problem is that there is no version of replace with signature :
    replace (size_type, size_type, E)
    instead there is
    replace (size_type, size_type,size_type, E)

    So, what does this mean? It means that you must specify the number of Es you put. In you case it's 1, because you want to replace one char by one char. If you put 2, then each lowercase would be replaced with 2 uppercase...
    Here's what you need :

    string allcaps (string inputStr)
    {
    // Scroll through each character in the string
    for (int charpos = 0; charpos < inputStr.length(); charpos++)
    {
    // Assign current character to temp char variable
    char selectedChar = inputStr.at(charpos);

    // Re-Assign upper-case equivalent of selectedChar to itself
    selectedChar = toupper(selectedChar);
    inputStr.replace(charpos, 1, 1, selectedChar);
    }

    return inputStr;
    }

  3. #3
    Unregistered
    Guest

    Smile

    Thanks so much, that really cleared things up!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21