我想要这个c++;程序查找以用户输入的字母开头的单词,并使用新功能打印它们 我希望这个C++程序能从用户输入的字母开始查找单词,并用一个新函数打印出来。但细细的是,它只在第二次循环中找到第一个字母,然后,我不知道会发生什么…我是初学者,请帮

我想要这个c++;程序查找以用户输入的字母开头的单词,并使用新功能打印它们 我希望这个C++程序能从用户输入的字母开始查找单词,并用一个新函数打印出来。但细细的是,它只在第二次循环中找到第一个字母,然后,我不知道会发生什么…我是初学者,请帮,c++,C++,我想要这个c++;程序查找以用户输入的字母开头的单词,并使用新功能打印它们 我希望这个C++程序能从用户输入的字母开始查找单词,并用一个新函数打印出来。但细细的是,它只在第二次循环中找到第一个字母,然后,我不知道会发生什么…我是初学者,请帮帮我! 取消注释必要的行 #include<iostream> #include<fstream> #include<string> using namespace std; void getUserInp

我想要这个c++;程序查找以用户输入的字母开头的单词,并使用新功能打印它们 我希望这个C++程序能从用户输入的字母开始查找单词,并用一个新函数打印出来。但细细的是,它只在第二次循环中找到第一个字母,然后,我不知道会发生什么…我是初学者,请帮帮我! 取消注释必要的行

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

void getUserInput(string *filename, string *find)
{
    cout << "file name : ";
    cin >> *filename;
    cout << "required character : ";
    cin >> *find;

}
string* processFile(string fileName, string word, int *t, int *w, string found[])
{
    fstream file;
    int countIn = 0,
        totaal = 0;
    int *count = &countIn;
    int *total = &totaal;
    int i = 0;
    string find;  // the max length of the file should not exceed this value is if does please change it here.
    file.open(fileName, ios::in);
    if (file.is_open())
    {
        while (!file.eof())
        {

            file >> find;
            totaal++;
            if (word == find)
            {
                char a[100];
                int s = find.size();
                for (int j = 0; i < find.size(); j++) 
                {
                    string(1, find[i]);
                }

                found[i] = find;
                i++;
                countIn++;
            }

        }
    }
    else
        cout << "!!!!invalid file name!!!!\n";
    file.close();
    //for (int i = 0, j = 0; i < totaal; i++)
    //{
    //  
    //  cout << find[i] << '\t' << find[i][0] << endl;
    //  
    //  if (word == find[i][0])
    //  {
    //      cout << "i is " << i << endl;
    //      cout << "j is " << j << endl;
    //      cout << "Calculated i and j\n";
    //      found[j] = find[i];
    //      cout << "found[j] " << found[j] << "\nfind[i] " << find[i] << endl;
    //      j++;
    //      countIn++;
    //      cout << "Inside if\n";
    //  }
    //  cout << "outsidenside if\n";
    //}
    *t = *total;
    *w = *count;
    //cout << countIn << endl << totaal << endl;
    //cout << *count << endl << *total<<endl;

    return found;

}
void displayOutput(int total, int count, string wordlist[])
{
    cout << "Total words in the file: " << total;
    if (count != 0)
    {
        cout << "\nTotal " << count << " related words found in the file\n";
        for (int i = 0; i < count; i++)
            cout << i + 1 << "\t" << wordlist[i] << endl;
    }
    else
        cout << "No matching case found\n";



}
int main()
{

    string nameoffile;
    string wordtofind;
    string *ptr1 = &nameoffile;
    string *ptr2 = &wordtofind;
    string foundwords[] = { "" };
    int occur = 0,
        totalWords = 0;
    int *occ = &occur;// occurence of the certain word
    int *tot = &totalWords;// total wods in the file
    getUserInput(ptr1, ptr2);

    processFile(nameoffile, wordtofind, occ, tot, foundwords);  //calling the processing function

    displayOutput(occur, totalWords, foundwords);

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
void getUserInput(字符串*文件名,字符串*查找)
{
cout>*文件名;
cout>*查找;
}
string*processFile(字符串文件名、字符串字、int*t、int*w、找到的字符串[])
{
流文件;
int countIn=0,
totaal=0;
int*count=&countIn;
整数*总计=&totaal;
int i=0;
string find;//文件的最大长度不应超过此值,如果是,请在此处更改。
打开(文件名,ios::in);
if(file.is_open())
{
而(!file.eof())
{
文件>>查找;
totaal++;
if(word==find)
{
chara[100];
int s=find.size();
对于(int j=0;i根据您的描述,为什么在一个完全不需要使用指针的程序中会使用指针呢?
while(!file.eof())
几乎肯定更喜欢通过引用传递
std::string
,或者如果您正在更改参数,则通过
const
引用传递。Paul如果我不使用指针,为什么我能够访问主函数或函数的任何其他主体中的char数组(在其中检索文本)。假设我不能使用global变量。很抱歉,我之前没有提到。根据您的描述,为什么在一个完全不需要使用指针的程序中使用指针?
while(!file.eof())
几乎肯定更喜欢通过引用传递
std::string
,或者如果您正在更改参数,则通过
const
引用传递。Paul如果我不使用指针,为什么我能够访问主函数或函数的任何其他主体中的char数组(在其中检索文本)。假设我不能使用global变量。对不起,我刚才没有提到。