【发布时间】:2017-01-31 01:28:39
【问题描述】:
#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;
}
我想考虑一下我对这段代码的意图很清楚。只需取一个句子,用该字符+后缀和前缀替换所有字符。
我的问题是,在调试器中运行时,我会输入"hello world",程序会输出"osss"。
我完全没有 C++ 方面的教育,在这里完全不知所措。是我的++i吗?
【问题讨论】:
-
cin >> words;只会读取一个单词,而不是一行中的所有单词。 -
您不能使用
+连接字符串文字和字符。其中一个参数必须是std::string。