如何在C ++中调试我的单词搜索代码?

时间:2016-04-11 01:13:05

标签: c++

我需要一些关于我的C ++单词搜索代码的帮助。我得到了它的工作,但不是我想要的。

当我运行代码时,它扫描我输入的文本文件,只输出文本文件中与我的代码中的数组匹配的一个单词。当我将其他单词添加到数组中的文本文件时,它会给我一个错误。

有人可以帮我改一下c ++代码吗?

这就是我想要的样子:

This is what i want to look like

这就是我得到的:

And this is what i got

#include <fstream>
#include <iostream>
#include <cmath>
#include <iomanip>


#define SIZE 30
using namespace std;



const char *Phish[SIZE] ={"Amazon","official","bank","security",          
"urgent","Alert","important","inform  ation", "ebay", "password", "credit", "verify",
"confirm", "account","bill", "immediately", "address", "telephone","SSN", "charity",
"check", "secure", "personal", "confidential",
"ATM", "warning","fraud","Citibank","IRS", "paypal"};

int point[SIZE] =      {2,2,1,1,1,1,2,2,3,3,3,1,1,1,1,1,2,2,3,2,1,1,1,1,2,2,2,2,2,1};                                                                               
int totalPoints[SIZE];

void outputResults();

int main(void)
{
    FILE *cPtr;
    char filename[100];
    char message[5000];
    char *temp[100];
    int i;
    int counter=0;
    int words=0;
    char *tokenPtr;

    cout << "Enter the name of the file to be read: \n";
    cin >> filename;

    if ( (cPtr = fopen(filename,"rb")) == NULL)
    {
        cout <<"File cannot be opened.\n";
    }
    else
    {
        fgets(message, 5000, cPtr);
        tokenPtr = strtok(message, " ");
        temp[0] = tokenPtr;
        while (tokenPtr!=NULL)
        {
            for(i=0; i< SIZE; i++)
            {
                if(strncmp(temp[0], Phish[i], strlen(Phish[i]))==0)
                {
                    totalPoints[i]++;
                    break;
                }

                tokenPtr =strtok(NULL, " ");
                temp[0] = tokenPtr;
                words++;

            }
            outputResults();

            cout << "\n";
            return 0;


        }
    }
}

void outputResults()
{
    int i;
    int count =0;
    int a;
    cout<<left<<setw(5) << "WORD "
        << setw(7)<<"# OF OCCURRENCE "
        << setw(15)<<"POINT TOTAL";

    for(i=0; i<SIZE; i++)
    {
        if(totalPoints[i] !=0)
        {
            cout<<"\n"<<left << setw(10)<< Phish[i]
                << setw(11)<< totalPoints[i]
                << setw(13)<< point[i]*totalPoints[i];
            count += point[i] * totalPoints[i];
        }
    }
    cout<< "\nPoint total for entire file: \n"<< count;
}

0 个答案:

没有答案