I'm new at programming and I can't figure this out. Can anyone please help?

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

bool nocase_compare (char c1, char c2)
{
    return toupper(c1) == toupper(c2);
}   

int main( int argc, char* argv[] ) 
{

    ifstream infile; 
    string filename;

    do
    {
        cout << "File name? ";
        cin >> filename;
        infile.open(filename.c_str());
            if (!infile)
            { 
                cerr << "Cannot open file: " << filename << "\n" << endl;
                return 0;
            }

    } while (!infile);


    string line;
    string search;
    int counter = 0;
    size_t found=' ';

    cout << "Search string? ";
    cin >>search;

    string::iterator pos;
    pos = search (line.begin(),line.end(),
        search.begin(),
        search.end(),
        [COLOR="red"]nocase_compare);[/COLOR]  [COLOR="red"]<===Error[/COLOR]

    if (pos == line.end())
    {
        cout <<"not found";
    } 

    else
    {
        cout<<"found";
    }


    infile.close();
    system("pause");
    return 0;
}


error C2064: term does not evaluate to a function taking 5 arguments

Edited 3 Years Ago by mike_2000_17: Fixed formatting

1. you should #include<algorithm>

2. you create 'string line;' but then never assign anything to it..

3. so when you call search(line.begin(), line.end(), search.begin(), search.end(), nocase_compare), arguments #1 and #2 will return iterators to a string of nothing.


for more information: http://www.cplusplus.com/reference/algorithm/search/

Edited 6 Years Ago by Clinton Portis: n/a

the search function takes in 4 arguments not five.

What you want is something like this:

//returns true if source contains target with case ignored
bool no_case_compare(const string& source, const string& target){
 string srcCpy(source.size(),0);
 string targetCpy(target.size(),0);
 std::transform(source.begin(),source.end(),srcCpy.begin(),tolower);
 std::transform(target.begin(),target.end(),targetCpy.begin(),tolower);
 return srcCpy.find(targetCpy) != string::npos;
}

Edited 6 Years Ago by firstPerson: n/a

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