当我两次键入ask时,没有得到askyay askyay,我只得到了ask askyay

当我两次输入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;
}

最佳答案

用值替换变量,然后逐步执行代码。由于未拆分单词,然后追加了“yay”或“ay”,因此可以预期结果

new_word = message + "yay ";

将导致
new_word  = "ask ask" + "yay";


pig_message = message.substr(1) + message[0] + "ay";

将导致
pig_message  = "og dog" + "d" + "ay";

关于c++ - Pig Latin,用于循环,字符串问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40519387/

10-12 21:41