deptage
deptage

Reputation: 31

Unable to convert argument for string insert function

I need help with my homework. I have to write a program which multiplies random vowels in random numbers. Here's my code. I have included libraries: iostream, string, cstdlib and ctime. When I compile, i get the following error:

„std::_String_iterator<std::_String_val<std::_Simple_types<_Elem>>> std::basic_string<_Elem,std::char_traits<char>,std::allocator<char>>::insert(const std::_String_const_iterator<std::_String_val<std::_Simple_types<_Elem>>>,const unsigned int,const _Elem)”: nie można dokonać konwersji argumentu 1 z „int” do „const std::_String_const_iterator<std::_String_val<std::_Simple_types<_Elem>>>”
using namespace std;
int main() {
  srand(time(0));
  string x;
  getline(cin, x);

  for (int i = 0; i < x.size(); i++) {
    if (x[i] == 'a' || x[i] == 'e' || x[i] == 'i' || x[i] == 'o' ||
        x[i] == 'u' || x[i] == 'y' || x[i] == 'A' || x[i] == 'E' ||
        x[i] == 'I' || x[i] == 'O' || x[i] == 'U' || x[i] == 'Y') {
      int d = rand() % 5 + 1;
      for (int j = 0; j < d; j++) {
        x.insert(i, x[i]);
      }
    }
  }
  cout << x;
}

Upvotes: 2

Views: 105

Answers (3)

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29965

You can find std::string::insert overloads in this reference. None of the overloads are viable given the parameters you pass (int and char). Hence the error.

Upvotes: 1

user10957435
user10957435

Reputation:

The problem is that while there are many overloads to insert, none of them are what you are using.

Here are is what you should try instead:

x.insert(i, 1, x[i]);

Upvotes: 1

Jim Rhodes
Jim Rhodes

Reputation: 5085

There is a version of insert that takes a position, count and char. You will want to use that instead of for loop.

int d = rand() % 5 + 1;
x.insert(i, d, x[i]);

But I don't think your main loop will ever end because it will always match an inserted vowel.

Upvotes: 3

Related Questions