C++ 输出随机字符的字符串改变程序 #包括“stdafx.h” #包括 #包括 #包括 使用名称空间std; 字符串输出; 字符串; int i; int main() { cin>>words;//从用户获取单词 output=”“;//读取输出字符串 i=0;//预热计算器 int size=words.size();//大小很重要 而(i
这条线:C++ 输出随机字符的字符串改变程序 #包括“stdafx.h” #包括 #包括 #包括 使用名称空间std; 字符串输出; 字符串; int i; int main() { cin>>words;//从用户获取单词 output=”“;//读取输出字符串 i=0;//预热计算器 int size=words.size();//大小很重要 而(i,c++,string,input,output,C++,String,Input,Output,这条线: #include "stdafx.h" #include <iostream> #include <string> #include <algorithm> using namespace std; string output; string words; int i; int main() { cin >> words; // gets words from user output = ""; // readys t
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string output;
string words;
int i;
int main()
{
cin >> words; // gets words from user
output = ""; // readys the output string
i = 0; // warms up the calculator
int size = words.size(); // size matters
while (i <= size) { // loops through each character in "words" (can't increment in the function?)
output += ":regional_indicator_" + words[i] +':'; // appends output with each letter from words plus a suffix and prefix
++i;
}
cout << output << endl; // prints the output
return 0;
}
不起作用。字符串连接的+
运算符重载仅在其中一个参数是std::string
时有效。但您尝试将其与C字符串文字和字符一起使用。将其更改为:
output += ":regional_indicator_" + words[i] +':'; // appends output with each letter from words plus a suffix and prefix
这对每个部分使用+=
的std::string
重载,并执行您想要的操作
此外,如果您想阅读整行文字,而不仅仅是一个单词,请使用:
output += "regional_indicator_";
output += words[i];
output += ':';
cin>>words;
将只读取一个单词,而不是一行中的所有单词。您不能使用+
连接字符串文字和字符。其中一个参数必须是std::string
。非常感谢!工作非常完美,我在它周围抛出了一个if循环来处理空格。我将开始阅读Kenninghan和Ritchie今晚的我不必再问任何愚蠢的问题了。
getline(cin, words);