The program transfers part of the text to a new line if the number of characters exceeds the allowed value in the line.

Algorithm of the program: 1. There is a text. How It Works

  1. Enter the number of valid characters per line, I enter n = 4.

  2. Using the for loop, the program selects the first n characters and places them in a new type of string and searches among them for the possibility of fulfilling the division condition: two consecutive vowels can be divided if there is a consonant in front of the first, and at least one letter follows the second this is considered together with the preceding vowel as a whole). If there is no such possibility, the program stops the robot.

    I had a problem after the for loop where I want to put the first n characters in the new type string, the compiler compiles the program, but when it comes to the code section, the program crashes: cout << clone.insert(str.find(str[nom]), str);

The code itself:

 #include <iostream> #include <vector> #include <string> #include <windows.h> #include <sstream> #include <iterator> using namespace std; void delitel(string str, int i, int n); int main() { setlocale(LC_ALL, "Russian"); SetConsoleCP(1251); SetConsoleOutputCP(1251); vector<string> arr; string str("Привеет Как Дела"); string delim(" "); size_t prev = 0; size_t next; size_t delta = delim.length(); int i; cout << "Введи количество символов в строке: "; cin >> i; while ((next = str.find(delim, prev)) != string::npos) { //Отладка-start string tmp = str.substr(prev, next - prev); cout << tmp << " "; //Отладка-end arr.push_back(str.substr(prev, next - prev)); prev = next + delta; } //Отладка-start string tmp = str.substr(prev); cout << tmp << endl; //Отладка-end arr.push_back(str.substr(prev)); cout << endl; int n; n = str.size(); if (n > i) delitel(str, i, n); system("pause"); } void delitel(string str, int i, int n) { int nom; string clone; for (nom = 0; nom <= i; nom++) { cout << str[nom]; } cout << clone.insert(str.find(str[nom]), str); } 

    1 answer 1

    The clone object contains no data, or, otherwise, contains an empty string.

    Calling the find method for the string str ( str.find(str[nom]) ) in any case returns a value other than 0. Therefore, calling the insert method for the string clone

     cout << clone.insert(str.find(str[nom]), str); 

    will throw an exception because the specified insertion position is larger than the size (empty) of the string contained in the clone object.

    • void delitel (string str, int i, int n) {int nom; vector <string> text; for (nom = 0; nom <= i; nom ++) {text.push_back (str); } cout << text [i]; } Why the whole text is displayed if it should output the first 4 characters, with i = 4. - nicolia
    • @nicolia And in this sentence text.push_back (str); Did you really add to the vector not the whole line? - Vlad from Moscow