fivestar
fivestar

Reputation: 17

Pig Latin, for loop, string problems

When I type ask twice, instead of getting askyay askyay back i only get ask askyay.

When I enter dog twice, instead of getting ogday ogday I get og dogday.

I'm not sure what I'm doing wrong.

#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;
}

Upvotes: 1

Views: 98

Answers (1)

nobody
nobody

Reputation: 11080

Substitute the variables with the values and step through the code.The result is expected since you are not splitting the words and then appending "yay" or "ay"

new_word = message + "yay ";

will result into

new_word  = "ask ask" + "yay";

and

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

will result into

pig_message  = "og dog" + "d" + "ay";

Upvotes: 3

Related Questions