0

trying to take a string and replace the lowercase letters to specific numbers (conversions apparent in code) using a while loop. string.replace gives an error "no matching member function for call to 'replace' :| been staring at the string.replace reference and can't see how my formatting is wrong.

#include <iostream>
#include <cctype>
#include <string>

using namespace std;

int main() {

    // set variables

    string input_string;
    int string_length;
    bool isupper;
    bool isdig;
    int count;
    char current_var;
    string output_string;

    // Query user

    cout << "Welcome to Josh's Password Generator. Enter a password and I will convert it into numbers for entry in a touchtone phone." << endl;
    cin >> input_string;



    // calculations

    string_length = (int)input_string.length();
    count = 0;


    while(count <= string_length)

        if (input_string.at(count) == 'a' || input_string.at(count) == 'b' || input_string.at(count) == 'c')
    {
        current_var = 1;
    }
        else if (input_string.at(count) == 'd' || input_string.at(count) == 'e' || input_string.at(count) == 'f')
    {
       current_var = 2;
    }
    else if (input_string.at(count) == 'g' || input_string.at(count) == 'h' || input_string.at(count) == 'i')
    {
        current_var = 3;
    }
    else if (input_string.at(count) == 'j' || input_string.at(count) == 'k' || input_string.at(count) == 'l')
    {
        current_var = 4;
        }
        else if (input_string.at(count) == 'm' || input_string.at(count) == 'n' || input_string.at(count) == 'o')
        {
            current_var = 5;
        }
        else if (input_string.at(count) == 'p' || input_string.at(count) == 'q' || input_string.at(count) == 'r')
        {
            current_var = 6;
        }
        else if (input_string.at(count) == 's' || input_string.at(count) == 't' || input_string.at(count) == 'u')
        {
            current_var = 7;
        }
        else if (input_string.at(count) == 'v' || input_string.at(count) == 'w' || input_string.at(count) == 'x')
        {
            current_var = 8;
        }
        else if (input_string.at(count) == 'y' || input_string.at(count) == 'z')
        {
            current_var = 9;
        }
        else
            current_var = input_string.at(count);

        input_string = input_string.replace(count, current_var);

        count = count + 1;


    }
2
  • Apart for the fact that this code does not make a password less guessable, what you heard of either maps or switch? Commented Mar 7, 2016 at 22:15
  • std::string::replace is designed to substitute a substring with another string. Here, a simple input_string[count] = current_var; would suffice since you are only replacing a single character. Commented Mar 7, 2016 at 22:17

1 Answer 1

1

replace requires at least 3 arguments. Are you looking at the wrong documentation?

You're missing the length argument, which indicates how many characters in the string are to be replaced. Try instead:

input_string.replace(count, 1, string(current_var));

One other note - 'using namespace std;' is not advisable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.