【问题标题】:C++: English to Pig LatinC++:英语到猪拉丁语
【发布时间】:2020-07-08 22:00:16
【问题描述】:

当我尝试动态分配数组时,我收到来自以下代码的错误(在我尝试使用 bool 函数递增用户数组中的每个字母之后看到)。这是错误:

main.cpp:在函数‘Word* splitSentene(std::string, int&)’中: main.cpp:81:32:错误:无法在赋值中将“std::string* {aka std::basic_string*}”转换为“Word*” 词=新字符串[i];

我正在尝试计算用户输入的单词数量,并为字符串动态分配一个数组。到目前为止,这是我的代码:

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

struct Word 
{
    string english;     // English sentence 
    string piglatin;    // Pig latin sentence 
};

// PT 1. Function prototype 
Word * splitSentence(const string words, int &size){};



int main()
{
    string userSentence;
    int size; 
    
    // Get the users sentence to convert to pig latin 
    cout << "Please enter a string to convert to pig latin:\n";
    getline(cin, userSentence);

    // Directs to Word * splitSentence function 
    Word* tempptr = splitSentence(userSentence, size);
    delete [] tempptr;


    return 0;   
}

//PT 1. Analyze the sentence 
Word * splitSentene(const string words, int &size)
{
    bool flag = true;
    int num = 0;
    
    for (int i = 0; i < words.length() + 1; i++)
    {
        //test for white space, then when you hit the first alphabetical character after a space,
        //increment up the size of the array 
        if (isspace(words[i])) 
        
            flag = true;
            
        if (isalpha(words[i]));
        {
            if (flag == true)
            {
                flag = false;
                cout << words[i++];
            }
        }
        // Dynamically allocate the array for the words  
        Word *sentence = nullptr;
        sentence = new string[i];
    }


}

这里是 pt 1 的说明,用于进一步说明:

PT。 1) 编写一个函数,将一个英文句子作为一个字符串。这个函数应该首先计算句子中有多少“单词”(单词是由空格分隔的子字符串)。然后它应该分配一个大小等于字数的动态数组。该数组包含 Word 结构(即 Word 类型的数组)。然后该函数将该句子的每个单词存储到相应结构的英语字段中。然后,该函数应使用 return 语句将此数组以及使用引用参数的数组大小返回给调用函数。

此函数还应删除除字母之外的所有大写字母和特殊字符。使用以下原型实现函数:

Word * splitSentence(const string words, int &size);

这是我在这里的第一篇文章,因此我将感谢有关如何动态分配数组和格式化它的任何输入(如果我已经成功编码了如何计算用户输入的句子中的单词)。如果需要提供更多信息,请告诉我!

【问题讨论】:

  • 您正在尝试将std::string* 分配给Word*。如果你这样做sentence = new Word[i]会发生什么?
  • 指令并没有说你必须进行动态内存分配。如果可以,请避免使用newdelete
  • Word * splitSentence(const string words, int &amp;size){}; -- 我不知道你是否意识到这一点,但 C++ 允许你按值返回:Word splitSentence(const string words, int &amp;size); -- 然后只返回 Word 对象。不需要动态内存分配。
  • @cigen "指令并没有说你必须进行动态内存分配" - 是的,它确实:“它应该然后分配一个大小等于字数的动态数组”并且由于该函数返回一个原始的Word* 指针,因此基本上只剩下new[]malloc() 用于该分配。当然,您是对的,应该尽可能避免使用new。可以改用staticthread_local vector&lt;Word&gt; 并返回其data(),只要调用者知道返回的指针不指向delete[]
  • @RemyLebeau 哦,确实如此。我的眼睛一定在上面呆滞了:p

标签: c++


【解决方案1】:

编译器错误是因为您试图将string[] 数组分配给Word* 指针。您需要分配一个 Word[] 数组。

您的代码中还有其他错误:

  • splitSentence() 的声明末尾有一个错误的{}

  • 您在splitSentence() 的定义中拼错了splitSentene

  • 您在if (isalpha(words[i])); 上有一个错误的;

  • 你不是return'ing 你分配的数组。

事实上,您甚至根本没有正确遵循说明。在分配数组之前,您没有“计算句子中有多少单词”(您尝试过,但您在错误的位置进行分配),并且您根本没有填充数组,更不用说“删除所有大写和字母以外的特殊字符”。

试试这样的:

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

struct Word 
{
    string english;     // English sentence 
    string piglatin;    // Pig latin sentence 
};

// PT 1. Function prototype 
Word* splitSentence(const string words, int &size);

int main()
{
    string userSentence;
    int size; 
    
    // Get the users sentence to convert to pig latin 
    cout << "Please enter a string to convert to pig latin:\n";
    getline(cin, userSentence);

    // Directs to Word * splitSentence function 
    Word* tempptr = splitSentence(userSentence, size);
    delete [] tempptr;

    return 0;   
}

//PT 1. Analyze the sentence 
Word* splitSentence(const string words, int &size)
{
    bool flag = true;
    int num = 0;
    char ch;
    
    for (int i = 0; i < words.length(); ++i)
    {
        ch = words[i];

        if (isalpha(ch))
        {
            if (flag)
            {
                flag = false;
                ++num;
            }
        }
        else if (isspace(ch))
        {
            flag = true;
        }
    }

    Word *sentence = new Word[num];
    int index = -1;

    flag = true;
    num = 0;
    
    for (int i = 0; i < words.length(); ++i)
    {
        ch = words[i];

        if (isalpha(ch))
        {
            if (flag)
            {
                flag = false;
                ++num;
                ++index;
            }

            if (isupper(ch))
            {
                ch = tolower(ch);
            }

            sentence[index].english += ch;
        }
        else if (isspace(ch))
        {
            flag = true;
        }
    }

    size = num;
    return sentence;
}

Live Demo


话虽如此,如果您可以使用std::istringstreamstd::vector 以及其他C++ 习语,而不是使用C 习语,这将更容易实现splitSentence(),例如:

#include <iostream>
#include <sstream>
#include <cctype>
#include <string>
#include <vector>
#include <algorithm>
using namespace std; 
     
struct Word 
{
    string english;     // English sentence 
    string piglatin;    // Pig latin sentence 
};
     
// PT 1. Function prototype 
Word* splitSentence(const string words, int &size);
     
int main()
{
    string userSentence;
    int size; 
     
    // Get the users sentence to convert to pig latin 
    cout << "Please enter a string to convert to pig latin:\n";
    getline(cin, userSentence);
     
    // Directs to Word * splitSentence function 
    Word* tempptr = splitSentence(userSentence, size);
    delete [] tempptr;

    return 0;   
}
     
//PT 1. Analyze the sentence 
Word* splitSentence(const string words, int &size)
{
    istringstream iss(words);
    vector<string> vec;
    string s;
     
    while (iss >> s)
    {
        remove_if(s.begin(), s.end(),
            [](unsigned char ch){ return !isalpha(ch); });

        if (!s.empty())
        {
            transform(s.begin(), s.end(), s.begin(),
                [](unsigned char ch){ return tolower(ch); });

            vec.push_back(s);
        }
    }
     
    Word *sentence = new Word[vec.size()];
     
    transform(vec.begin(), vec.end(), sentence,
        [](const string &s){
            Word w;
            w.english = s;
            return w;
        }
    );
     
    size = vec.size();
    return sentence;
}

Live Demo

【讨论】:

    猜你喜欢
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    相关资源
    最近更新 更多