Home > Software engineering >  How to fix "a parameter list without types is only allowed in a function definition"
How to fix "a parameter list without types is only allowed in a function definition"

Time:10-30

I am trying to create a function that would take a name(string) that has been input by a user and compare it with a string within a custom data type/structure to verify that the name that the user has input exists in the memory.

The custom data type is set to be global and is as follows:

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct
{
    string name;
    int votes;
}
candidate;

// Array of candidates
candidate candidates[MAX];

The names of the different candidates are stored in the array candidates via a command line argument.

After that the user is prompted to vote for a candidate by inserting the name of a candiate using: string name = get_string("Vote: ");

which is then taken to this function to verify whether the candidate's name exists or not.

Here is the function:

bool vote(string name)
{
    // TODO
    int i;
    string cn = candidates[i].name;
    for (i = 1; i < MAX; i  )
    {
        int strcmp(cn, name);
    }

    return strcmp;
}

However, I am not sure what the error means:

a parameter list without types is only allowed in a function definition
        int strcmp(cn, name);
                   ^

How can I fix this?

CodePudding user response:

The strcmp() compares two strings character by character.

If the strings are equal, the function returns 0.

A simple way to use strcmp:

int returnValue = strcmp(cn, name);
if (returnValue == 0) {
    // string matched, so this name exists
}

Please check below 2 links to learn more about strcmp()

Another mistake to mention, you tried to access the ith candidate's name from outside the loop.

You need to do it from inside.

For example:

for(int i = 0; i < MAX; i  ) {
    string cn = candidates[i].name;
}
  • Related