1
#include <iostream>
#include <string>
using namespace std;

string Latin(string words)
{

string strWord, strSentence = "";
int length = 0, index = 0;
while (words[index] != '\0')
{
    if(words.find(' ', index) != -1)
    {
        length = words.find(' ', index);
        length -= index;
        strWord = words.substr(index,length);
        strWord.insert(length, "ay");
        strWord.insert(length, 1, words[index]);
        strWord.erase(0,1);
        index += length +1;
    }
    else
    {
        strWord = words.substr(index);
        length = strWord.length();
        strWord.insert(length, "ay");
        strWord.insert(length,1,words[index]);
        strWord.erase(0,1);
        index = words.length();
    }

    strSentence += (strWord + " ");
}

return strSentence;
}

int main()
{
    string str;
    getline(cin,str);
    str = Latin(str);
    cout<<str<<endl;


return 0;
}

I get this error that says http://imgur.com/OI0TgBG

I have no clue what to do. As I am new to this, this is a program that is suppose to ask for user input of a length of words and translate them into pig Latin. Any help would be greatly appreciated.

9
  • 3
    You should step through your code with the debugger and find out where it throws the exception. That will help you figure out what is wrong. Commented Dec 2, 2015 at 19:05
  • 5
    click "Retry" then see the call stack on the debugger Commented Dec 2, 2015 at 19:05
  • 1
    @erip: actually, std::strings are null terminated! See 21.4.5 [string.access] paragraphs 1 and 2 which essentially state that s.[s.size()] == cT() for s being of type std::basic_string<cT>. Commented Dec 2, 2015 at 19:19
  • 1
    Note that find() return std::string::npos when it fails to find a suitable location. There is no guarantee that npos has the value -1. Commented Dec 2, 2015 at 19:22
  • 1
    @DietmarKühl Actually npos is declared as static const size_type npos = -1; in 21.4(5) [basic.string] Commented Dec 2, 2015 at 19:32

1 Answer 1

1

Unless I really wanted to make my own life difficult, I'd do this quite a bit differently. First, I'd use a std::stringstream to break the input string into words to process. Then, I'd use std::rotate to move the first character of the string to the end. Finally, I'd wrap that all in std::transform to manage applying the function to each word in succession.

std::string line;
std::getline(std::cin, line);

std::stringstream buffer(line);
std::stringstream result;

std::transform(std::istream_iterator<std::string>(buffer),
    std::istream_iterator<std::string>(),
    std::ostream_iterator<std::string>(result, " "),
    [](std::string s) { 
        std::rotate(s.begin(), s.begin() + 1, s.end()); 
        s += "ay"; 
        return s; 
    });

Of course, this doesn't know the special rules for things like words that start with vowels or letter pairs like sh or ch, but it looks like that's outside the scope of the task at hand.

For more on std::rotate, I recommend watching some of Sean Parent's videos.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.