我在C+中有一个插入运算符错误+; 我已经制作了一个C++程序,它可以是正确的,也可以是左对齐的。 但底部出现了一个关于“”的错误。接线员。 我还提供了一个图像来显示我所面临的错误。 我需要解决这个问题 如果有人能帮助我,我将不胜感激
我在C+中有一个插入运算符错误+; 我已经制作了一个C++程序,它可以是正确的,也可以是左对齐的。 但底部出现了一个关于“”的错误。接线员。 我还提供了一个图像来显示我所面临的错误。 我需要解决这个问题 如果有人能帮助我,我将不胜感激,c++,visual-studio,function,loops,cout,C++,Visual Studio,Function,Loops,Cout,#包括 #包括 使用名称空间std; 字符串左缩进(字符串和输入,常量整数长度){ bool-inword=false; 字符串输出; 字符串字; 加整数=0; 对于(int i=0;i长度){ //如果一个单词大于一行,会发生什么? } 添加+=单词长度(); 如果(添加>长度){ output.append(1,'\n'); 输出。追加(word); added=word.length(); 单词=”; } 否则{ 输出。追加(word); 单词=”; } 如果(添加+1长度){ //如果一
#包括
#包括
使用名称空间std;
字符串左缩进(字符串和输入,常量整数长度){
bool-inword=false;
字符串输出;
字符串字;
加整数=0;
对于(int i=0;i长度){
//如果一个单词大于一行,会发生什么?
}
添加+=单词长度();
如果(添加>长度){
output.append(1,'\n');
输出。追加(word);
added=word.length();
单词=”;
}
否则{
输出。追加(word);
单词=”;
}
如果(添加+1长度){
//如果一个单词大于一行,会发生什么?
}
else if(word.length()+added>length){
output.append(1,'\n');
输出。追加(word);
}
否则{
输出。追加(word);
}
}
返回输出;
}
字符串右缩进(字符串和输入,常量整数长度){
bool-inword=false;
字符串输出;
字符串字;
弦线;
对于(int i=0;i长度){
//如果一个单词大于一行,会发生什么?
}
if(line.length()+word.length()>长度){
如果(line.back()='')line.erase(line.length()-1);
对于(intj=0;j长度){
如果(line.back()='')line.erase(line.length()-1);
对于(intj=0;j cout您需要包含标题
#include <string>
#包括
而不是
#include <cstring>
#包括
“警告C4018:”
#include <cstring>
#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;
}