PigLatin C ++功能

时间:2014-01-06 14:16:40

标签: c++ string function

这段代码给了我很多奇怪的错误。无论出于何种原因,“newstring”函数未运行。我认为它可能与它是cout语句的一部分有关,因为如果我没记错的话,如果我独立于cout语句调用该函数,它就不会给出相同的错误。该程序需要一个字符串函数,但新函数由于某种原因没有运行。任何人都可以看看代码?

    #include <iostream>
    #include <string>

    using namespace std;

    void newstring(string);
    bool isVowel(char ch);
    string rotate(string pStr);
    string pigLatinString(string pStr);

    int main()
    {
        string str;

        cout << "Enter a sentence to be translated to Pig Latin: ";
        getline(cin, str);
        cout << endl;

        cout << "The pig Latin form of " << str << " is: " << newstring(str);

        system("PAUSE");
        return 0;

    }

    bool isVowel(char ch)
    {
        switch(ch)
        {
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
            case 'Y':
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'y':
                return true;
            default: 
                return false;
        }
    }
    string rotate(string pStr)
    {
        string::size_type len = pStr.length();
        string rStr;
        rStr = pStr.substr(1, len - 1) + pStr[0];
        return rStr;
    }       
    string pigLatinString(string pStr)
    {
        string :: size_type len;
        bool foundVowel;
        if (isVowel(pStr[0]))
            pStr = pStr + "-way";
        else 
            {
                pStr = pStr + '-';
                pStr = rotate(pStr);
                len = pStr.length();
                foundVowel = false;

            for ( int counter = 1; counter < len - 1; counter++)
                {
                      if (isVowel(pStr[0]))
                {
                    foundVowel = true;
                    break;
                }
                else 
                    pStr = rotate(pStr);

                if (!foundVowel)
                    pStr = pStr.substr(1, len) + "-way";
                    else    
                    pStr = pStr + "ay";
                }
                return pStr;
            }
    }

    string newstring(string sentence)
    {   
        string newsentence, currentword;

        for (int i = 0; i < sentence.length(); i++)
        {
            if (sentence[i]==' ')
            {
                pigLatinString(currentword)+" ";
                currentword.clear();
            }
            else
            {
                currentword+=sentence[i];
            }
        }
        return newsentence;
    }

3 个答案:

答案 0 :(得分:1)

您的newstring原型错误。

void newstring(string);

应该是

string newstring(string);

答案 1 :(得分:0)

函数新闻字符串被声明为具有类型void

void newstring(string);

您可能无法创建void类型的对象并将其发送到输出流

cout << "The pig Latin form of " << str << " is: " << newstring(str);

此函数也没有定义,因为您定义了另一个具有相同名称但返回std :: string

的函数
string newstring(string sentence)
                                                   ^^^^^^^^^^^^^^^^^^

答案 2 :(得分:0)

pigLatinString(currentword)+" ";

pigLatinString返回一个字符串,但是你没有对该结果做任何事情。

newstring返回newsentence,但是是空的。

也许你应该用pigLatinString返回的内容填充新的内容?

哦,现在我注意到你有两个newstring s ...一个空洞和一个字符串......