0

对不起,如果这含糊不清我对编程还是很陌生(对论坛也是新手>_>)

好的,我的代码应该从文件中读取一个数字,然后使用该数字读取该数量的单词作为字典单词。然后我将这些单词存储到一个数组中并保留它们以供以后使用。在文件中的字典单词出现某个段落之后,我将其读入并将其设置为 c 字符串数组。(到目前为止,我已经全部记下来了)但是对于程序的最后一部分,我需要返回该段落 c -string 并计算每个字典单词出现的次数。我目前正在尝试 para.find (word[0]) 但我遇到了一些我不知道如何修复的错误。

错误:|40|错误:在'paragraph'中请求成员'find',它是非类类型'char [2000]'|

代码:

#include <iostream>
#include <fstream>
#include <cstring>
#include <windows.h>

using namespace std;

int main()
{
    ifstream inStream;  //declare ifstream

    inStream.open("infile2.txt"); //open my file

    int number;                // number at the begining of the file that lets the     program know
    inStream >> number;        // how many dictionary words are to be expected.
    cout  << number << " dictionary word(s)" << endl << endl;

    char dict[30];
    char text[2000];
    char paragraph[2000];      // declareing some stuff
    int count;
    int position;
    string word[5];

    for (int i=0; i<number; i++)   // using c string to set the 'number' amount of words in the dict array
    {
        inStream.getline(dict,30,'|');
        word[i] = dict;
    }

    for (int i=0; i<number; i++)  // cout so i can see its all set up right.
    {
        cout << "word " << i+1 << " is: " << word[i] << endl;
    }
    cout << endl;

    inStream.get(paragraph,2000,'|'); // setting the rest of the paragrapg of the txt   document to a c string
    cout << paragraph;                // so it can be searched later using the 'dict' words

   position = paragraph.find (word[0]);   // trying to find the position of the first word stored in 'dict[0]' but i run into an error

    return 0;
}

infile2.txt 看起来像这样:

3牛排|鸡蛋|和|

牛排和鸡蛋,鸡蛋和牛排,鸡蛋和牛排,牛排和鸡蛋……

可口的。

4

1 回答 1

1

c-strings 不是类,也没有 find 方法(或任何其他方法),即paragraph.find. 您可以尝试使用字符串,或者如果您需要使用 c-strings,则可以使用一个 find 方法,该方法将两个 c 字符串作为参数。

比如这个

于 2011-11-29T00:08:00.880 回答