0

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.

Deidrei
  • 2,125
  • 1
  • 14
  • 14
  • 1
    [`std::regex`](http://en.cppreference.com/w/cpp/regex) is the canonical answer. Check your compiler's support first, though – sehe Dec 04 '13 at 07:59
  • Why is `year2` an array while `year1` is not? Why is there a `for` cycle when there is a `return` in every branch, so it runs just once? – Melebius Dec 04 '13 at 08:01
  • Yes, that was the goal anyways. It might be called multiple times, so whenever it needs to search for the year, it just starts at i = 0 then runs through the if else statments to find the condition that works, if it doesn't find anything it will just return false. – user3064777 Dec 04 '13 at 08:15
  • Word of advice; capitalise your type names before things get very confusing! :) – OMGtechy Dec 04 '13 at 08:37
  • That makes a lot of sense, thank you. I can see how I could confuse myself and end up changing valid code on the same line when debugging. – user3064777 Dec 04 '13 at 08:47

3 Answers3

1

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

Community
  • 1
  • 1
Saeed
  • 7,262
  • 14
  • 43
  • 63
1

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";
kunal
  • 956
  • 9
  • 16
  • This worked to get rid of the error message, thank you so much. I can't vote up, I don't have enough reputation on here, or I would. It seems I still have a logic error, I will investigate it further. – user3064777 Dec 04 '13 at 08:41
0

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.

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
  • Your answer is exactly what I understand the problem to be. I am having trouble translating that into working code I thought I needed to have a comparison for both cases, if wildcards were used or not. Would you use a strncmp? I am limited to what is in the libraries I'm using. I was under the impression that making it a type defined string "year"included yearLength by nature, I may be horribly wrong and I accept this haha. – user3064777 Dec 04 '13 at 08:20
  • @user3064777 I wouldn't use `strncmp` or any other library function because you want to have tight control over whether two characters are considered equal or not. I would write a simple `for` loop which (after verifying that both strings are the same length!) increases a counter and then use that counter as an index into both strings to get the character at the same position. If the character in the second argument is no "?" but different from the character in the first argument, then the two strings differ. If you execute the loop all the way to the end, the two strings are equal. – Frerich Raabe Dec 04 '13 at 09:01