0

I created a program that converts English words into pig Latin (part of my class). Currently it converts only one word: "hello"->"elloyay".

Out of my own curiosity I wanted to enable the possiblity reading multiple words separated by spaces and then have each one of these words converted accordingly.

More technically, use substr to grab a word between two sequential spaces. the input will be broken by substr to separated words. Each word in a time will be parsed by my make_pig_latin parser and will replace her corresponding word in that string array.

e.g.: the input "hello yellow fellow" will result in the output "elloyay ellowyay ellowyay".

Can anyone let me know if my coding to accomplish this task is on the right track. I keep crashing in runtime and I think it is due to the array of strings not being created correctly. But I am not fully sure about it.

Any help would be appreciated.

int main()
{
    string word;

    string word_List[] = { word };

    cout << "Enter your story (Please include spaces between each word): ";
    getline(cin, word);

    char c = word[0];

    int i = 0;
    int j = 0;
    int k = i;
    int l = 0;
    while (i < (word.length() - 1))
    {
        if (word[i] = '\n')
        {
            string new_Word = word.substr(j, k);
            string test_Pig = make_Pig_Latin(new_Word);

            word_List[l] = test_Pig;
            l == l + 1;
            j == i + 1;
            i == k + 1;
        }
        if (word[i] = '.')
        {
            i = word.length() + 1;
        }
    }
    cout << "The Story In Pig Latin Is " << word_List << endl;

    cin.ignore();
    cin.get();
    return EXIT_SUCCESS;
}

Extra information for the user to add: complete error line, compiler+version used, OS used.

2
  • 2
    Use a std::vector. word_List can only hold a single string, raw arrays have a fixed compile time size. Commented Apr 4, 2016 at 4:07
  • This kind of thing if(word[i] = '.') should be if(word[i] == '.'). And your array only has one slot. Commented Apr 4, 2016 at 4:42

1 Answer 1

2

if (word[i] = '\n') will set word[i] to '\n'. You probably mean to test if(word[i] == '\n')...

However, you are getting input one line at the time, there is no new line in between.

You could instead break the text by testing for blank space if(word[i] == ' ')...

There happens to be an easier way. Use std::stringstream to extract words. Use std::vector to make array of string (or vector rather). Example:

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

int main()
{
    std::string sentence = "How do you add strings to an array of strings";
    std::vector<std::string> vs;
    std::stringstream iss(sentence);

    std::string word;
    while (std::getline(iss, word, ' '))
        vs.push_back(word);

    for (auto wrd : vs)
        cout << wrd << "\n";

    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for that example. I am going to try it out, want to read up on vectors first to fully understand what they do, fairly new to this programming world. I will update if i can get it to work. thanks for your knowledge.

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.