猪拉丁C++功能

PigLatin C++ function

本文关键字:功能 C++      更新时间:2023-10-16

这段代码给了我很多奇怪的错误。无论出于何种原因,"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;
    }

您的newstring原型是错误的。

void newstring(string);

应该是

string newstring(string);

函数 newstring 被声明为具有 void 类型

void newstring(string);

您不能创建 void 类型的对象并在输出流中发送它们

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

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

string newstring(string sentence)
                                                   ^^^^^^^^^^^^^^^^^^
pigLatinString(currentword)+" ";

pigLatinString 返回一个字符串,但你不会对该结果执行任何操作。

newstring 返回 newSentence,但 is 为空。

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

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