1
当我键入ask
两次,而不是得到askyay askyay
回我只得到ask askyay
。猪拉丁语,for循环,字符串问题
当我输入dog
两次,而不是得到ogday ogday
我得到og dogday
。
我不知道我在做什么错。
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>
using namespace std;
int main()
{
string vowels = "aeiou";
string new_word;
string pig_message;
string message;
getline(cin, message);
for (unsigned int i = 0; i <= vowels.length(); i++)
{
if (message[0] == vowels[i])
{
new_word = message + "yay ";
cout << new_word;
}
else if (!message[0] == vowels[i])
{
pig_message = message.substr(1) + message[0] + "ay";
cout << pig_message;
}
}
system("pause");
return 0;
}
'std :: getline()'读取整行,包括空格。就我个人而言,我喜欢这种行为,但你必须记得处理这些空间。或者你可以使用'std :: cin >> message'来读取空格分隔的单词。 –