У меня ошибка оператора вставки в C ++ - PullRequest
0 голосов
/ 07 октября 2019

Я создал программу на С ++, которая может быть либо по правому краю, либо по левому краю, но в нижней части появляется ошибка, касающаяся "<", "<<", ">", ">>". операторы. Я также предоставил изображение, чтобы показать, с какими ошибками я сталкиваюсь. Мне нужно решить эту проблему, если кто-то может мне помочь, это было бы очень признательно.

Вот изображение ошибки

#include <iostream>
#include <cstring>
using namespace std;
string leftIndent(string &input, const int length) {
bool inword = false;
string output;
string word;
int added = 0;
for (int i = 0; i < input.length(); i++) {
    if (input[i] == ' ') {
        if (inword == true) {
            if (word.length() > length) {
                // What happen if a word larger than a line?
            }
            added += word.length();
            if (added > length) {
                output.append(1, '\n');
                output.append(word);
                added = word.length();
                word = "";
            }
            else {
                output.append(word);
                word = "";
            }
            if (added + 1 <= length) {
                output.append(1, ' ');
                added++;
            }
        }
        inword = false;
    }
    else {
        word += input[i];
        inword = true;
    }
}

if (!word.empty()) {
    if (word.length() > length) {
        // What happen if a word larger than a line?
    }
    else if (word.length() + added > length) {
        output.append(1, '\n');
        output.append(word);
    }
    else {
        output.append(word);
    }
}

return output;
}

string rightIndent(string &input, const int length) {
bool inword = false;
string output;
string word;
string line;
for (int i = 0; i < input.length(); i++) {
    if (input[i] == ' ') {
        if (inword == true) {
            if (word.length() > length) {
                // What happen if a word larger than a line?
            }
            if (line.length() + word.length() > length) {
                if (line.back() == ' ') line.erase(line.length() - 1);
                for (int j = 0; j < length - line.length(); j++) output.append(1, ' ');

                output.append(line);
                output.append(1, '\n');
                line = word;
                word = "";
            }
            else {
                line.append(word);
                word = "";
            }
            if (line.length() + 1 <= length) {
                line.append(1, ' ');
            }
        }
        inword = false;
    }
    else {
        word += input[i];
        inword = true;
    }
}
if (!word.empty()) {
    if (word.length() > length) {
        // What happen if a word larger than a line?
    }
    else if (line.length() + word.length() > length) {
        if (line.back() == ' ') line.erase(line.length() - 1);

        for (int j = 0; j < length - line.length(); j++) output.append(1, ' ');
        output.append(line);
        output.append(1, '\n');

        for (int j = 0; j < length - word.length(); j++) output.append(1, ' ');
        output.append(word);
    }
    else {
        line.append(word);

        for (int j = 0; j < length - line.length(); j++) output.append(1, ' ');
        output.append(line);
    }
}
return output;
}
int main() 
{
string str = "";
cout << leftIndent(str, 20);
cout << endl << endl;
cout << rightIndent(str, 20);
return 0;
}

Ответы [ 2 ]

0 голосов
/ 08 октября 2019

"предупреждение C4018: '<': несоответствие подписи / неподписания" Это предупреждение указывает, что вы делаете что-то по типу "int <unsigned int", прямо или неявно, передавая функцию, которая ожидает либо с противоположным. </p>

Я предлагаю вам попробовать использовать "size_t" вместо "int". size_t (он существует в C ++, а не в C), что соответствует некоторому «обычному» типу без знака, то есть без знака int.

Вот код:

#include <iostream>
#include <string>
using namespace std;
string leftIndent(string &input, size_t length) {
    bool inword = false;
    string output;
    string word;
    size_t added = 0;
    for (size_t i = 0; i < input.length(); i++) {
        if (input[i] == ' ') {
            if (inword == true) {
                if ( word.length() > length) {
                    // What happen if a word larger than a line?
                }
                added += word.length();
                if (added > length) {
                    output.append(1, '\n');
                    output.append(word);
                    added = word.length();
                    word = "";
                }
                else {
                    output.append(word);
                    word = "";
                }
                if (added + 1 <= length) {
                    output.append(1, ' ');
                    added++;
                }
            }
            inword = false;
        }
        else {
            word += input[i];
            inword = true;
        }
    }

    if (!word.empty()) {
        if ( word.length() > length) {
            // What happen if a word larger than a line?
        }
        else if ( word.length() + added > length) {
            output.append(1, '\n');
            output.append(word);
        }
        else {
            output.append(word);
        }
    }

    return output;
}

string rightIndent(string &input, size_t length) {
    bool inword = false;
    string output;
    string word;
    string line;
    for (size_t i = 0; i < input.length(); i++) {
        if (input[i] == ' ') {
            if (inword == true) {
                if (word.length() > length) {
                    // What happen if a word larger than a line?
                }
                if (line.length() + word.length() > length) {
                    if (line.back() == ' ') line.erase(line.length() - 1);
                    for (size_t j = 0; j < length - line.length(); j++) output.append(1, ' ');

                    output.append(line);
                    output.append(1, '\n');
                    line = word;
                    word = "";
                }
                else {
                    line.append(word);
                    word = "";
                }
                if ( line.length() + 1 <= length) {
                    line.append(1, ' ');
                }
            }
            inword = false;
        }
        else {
            word += input[i];
            inword = true;
        }
    }
    if (!word.empty()) {
        if ( word.length() > length) {
            // What happen if a word larger than a line?
        }
        else if ( line.length() + word.length() > length) {
            if (line.back() == ' ') line.erase(line.length() - 1);

            for (size_t j = 0; j < length - line.length(); j++) output.append(1, ' ');
            output.append(line);
            output.append(1, '\n');

            for (size_t j = 0; j < length - word.length(); j++) output.append(1, ' ');
            output.append(word);
        }
        else {
            line.append(word);

            for (size_t j = 0; j < length - line.length(); j++) output.append(1, ' ');
            output.append(line);
        }
    }
    return output;
}
int main()
{
    string str = "";
    cout << leftIndent(str, 20);
    cout << endl << endl;
    cout << rightIndent(str, 20);
    return 0;
}
0 голосов
/ 07 октября 2019

Вам нужно включить заголовок

#include <string>

вместо

#include <cstring>
...