-2

Currently going thru a c++ course. I had to create a word cipher using the strings: alphabet and key. to cipher an inputted word with less code as possible I created this solution that gives the error:

no matching function for call to std::basic_string<char>::find(std::string&, int&, int)

I don't know how to solve it, neither do I know if my idea would work at all, would LOVE some help. Thanks for your attention :)

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {

    string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
    string key  {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
    string word_to_encrypt {};

    getline (cin,word_to_encrypt);

    for (int i=0;i<word_to_encrypt.size;i++){
        word_to_encrypt.replace (i,1,key,(alphabet.find(word_to_encrypt,i,1)),1);
    }

    cout<< word_to_encrypt;
}
Yash
  • 11,486
  • 4
  • 19
  • 35
  • Please avoid `using namespace std;`. It is considered bad practice and will ruin your life. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Aug 04 '19 at 03:36

1 Answers1

2

Two problems:

First size is a function and not a variable. Therefore you need size().

Secondly std::string::find() has no overload which takes a std::string and two ints: https://en.cppreference.com/w/cpp/string/basic_string/find , but you can use the overload which takes a CharT instead by adding .c_str() or .data().

This compiles at least:

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {

    string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
    string key  {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
    string word_to_encrypt {};

    getline (cin,word_to_encrypt);

    for (int i=0;i<word_to_encrypt.size();i++){
        word_to_encrypt.replace(i, 1, key, (
            alphabet.find(word_to_encrypt.c_str(), i, 1)),1);
    }

    cout<< word_to_encrypt;
}
Michael Mahn
  • 737
  • 4
  • 11