Start New Discussion within our Software Development Community
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main ()
{
    int Count= 0,
        oldCount,
        lineNo= 0,
        pos;
    string searchStr,
           testStr,
           testSearch,
           fileName,
           intStr;

    cout << "File name? ";
    getline(cin,fileName);
    ifstream fin(fileName.c_str());;

    if (!fin)
    {
        perror(fileName.c_str());
        exit(1);
    }

    cout<<"Search string? ";
    getline (cin, searchStr);
    testSearch = searchStr;

    for (int i=0; i<testSearch.size(); i++)
    {
        testSearch[i]=tolower(testSearch[i]);
    }

    while (getline (fin, intStr))
    {
        testStr=intStr;

        for (int i=0; i=testStr.size(); i++)
        {
            testStr[i]= tolower(testStr[i]);
        }

        lineNo++;
        oldCount=Count;
        pos=0;

            while((pos=testStr.find(testSearch, pos)) < testStr.size())
            {
                pos++;
                Count;
            }
            if (Count>oldCount)
            {
                cout<<setw(5)<<lineNo<<": "<<intStr<<endl;
            }
    }
    cout <<"There were" <<Count <<"occurances of "<<searchStr<<" in " <<fileName<<endl;
    fin.close();
    return 0;
}

Both find() and size() return an item of datatype "size_t," which means what it actually returns is defined from system to system, but normally is an unsigned int. Make i and pos of type size_t.

For next time, please list what the warning is so someone doesn't have to guess.

This article has been dead for over six months. Start a new discussion instead.