comparing strings c++ with wildcard values

2.1k views Asked by At

I've been lurking around here for a long time, thank you for all your help in the past, even if this is the first question I've had to ask.

I'm trying to make a simple database program, and I am stuck the search requirement for it. When searching, the user needs to be able to enter a question mark if they don't know a value. If you knew a movie was from the 90's you could enter 199? and it would find all the movies that matched 199_. I keep getting errors when I compile, "cannot convert 'char*' to 'char ()[5] for argument '2' to 'bool compareYears(const char, char (*)[5]" I am trying to figure most of it out on my own, and I like to separate the functions and make them work in a separate .cpp file before adding them to the main file, just to make debugging easier.

#include <iostream>
#include <cstring>
#include <fstream>
#include <cctype>
using namespace std;

const int yearLength = 4;
typedef char year[yearLength + 1];

bool compareYears(const year year1, year year2[]);

int main()
{
    year year1 = "1992"; //year from database, will be assigned the 
                         //  variable when implemented in main program. 
    year year2;          //year entered by user, it will be compared to year1.

    cout << "Enter a year to search for: ";
    cin >> year2;
    cout << endl;

    if((compareYears(year1, year2)) == true)
        cout << "they match\n";
    if((compareYears(year1, year2)) == true)
        cout << "they do not match\n";

    return 0;
}

bool compareYears(const year year1, year year2[])
{
    for(int i = 0; i < 4; i++)
    {
        if (strncom(year1, year2[i], 4) ==0)
            return true;
        else if (strncmp(year1, "????", 4) == 0)
            return true;
        else
            return false;
    }
}

Thanks for helping me out with this, usually the most help I get from others is useless or insulting. What I need help with most is getting rid of that compiler error. I cannot figure it out for the life of me.

3

There are 3 answers

2
Frerich Raabe On

The type of the year2 argument for compareYears looks strange. It looks like this argument is the mask to test against, i.e. a literal year like 1992 or something using wildcards. Hence, how about making it a char array of yearLength bytes?

It might be even easier to write just a generic function which is given two strings or arbitrary length (the second of which may use wildcards) and sees whether they are equal. The quality test could first see whether both strings are the same length and if so, whether the character at every position in both strings is either equal or the character at the given position in the second string is a '?'. You could use a single loop to walk over both strings in parallel to do this.

0
Saeed On

First of all read this: typedef fixed length array

Then, use this typedef:

typedef struct year { char characters[4]; } year;

and change the code this way:

int main()
{
    year year1; 
    year1.characters= "1992"; //year from database, will be assigned the variable when implemented in main program. 
    year year2;          //year entered by user, it will be compared to year1.

    cout << "Enter a year to search for: ";
    cin >> year2.characters;
    cout << endl;

    if((compareYears(year1, year2)) == true)
        cout << "they match\n";
    else
        cout << "they do not match\n";

    return 0;
}

bool compareYears(year year1, year year2)
{
    if (strncom(year1.characters, year2.characters, 4) ==0)
        return true;
    else if (strncmp(year1, "????", 4) == 0)
        return true;
    return false;
}

I also fixed some logical bugs

1
kunal On

Just change these lines and it will work...the function declaration needs an array of year and you are trying to pass a variable..

if((compareYears(year1, &year2)) == true) //this changed from year2 to &year2
cout << "they match\n";
if((compareYears(year1, &year2)) == true) //this changed from year2 to &year2
cout << "they do not match\n";