Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9028631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:02:08+00:00 2026-06-16T07:02:08+00:00

I have this database and I Need to check whether a Product Name is

  • 0

I have this database and I Need to check whether a Product Name is already in the database otherwise I ask the user to input another one.

The problem is this:

I’m trying to compare a string (the Product Name) found inside the struct with the string the user inputs.

The coding of the struct, the user input part and the search method are here below:

product Structure

typedef struct
{
    char    pName[100];
    char    pDescription [100];
    float   pPrice;
    int     pStock;
    int     pOrder;
}product;

the checkProduct method:

int checkProduct (char nameCheck[100])
{
    product temp;
    p.pName = nameCheck;

    rewind (pfp);
    while (fread(&temp,STRUCTSIZE,1,pfp)==1)
    {
        if (strcmp (temp.pName,p.pName))
        {
            return 1;
        }
    }
    return 0;
}

and the user input part [part of the code]:

char nameCheck[100];
gets (nameCheck);
checkProduct (nameCheck);
while (checkProduct == 1)
{
    printf ("Product Already Exists!\n Enter another!\n");
    while (getchar() !='\n')
    {
        continue;
    }
}
p.pName = nameCheck;

Now I am having the following errors (I Use ECLIPSE):

on the line
while (checkProduct == 1) [found in the user input] is giving me:
“comparison between pointer and integer – enabled by default” marked by a yellow warning triangle

p.pName = nameCheck; is marked as a red cross and stopping my compiling saying:
“incompatible types when assigning to type ‘char [100] from type ‘char*’
^—- Is giving me trouble BOTH in the userinput AND when I’m comparing strings.

Any suggestions how I can fix it or maybe how I can deference it? I can’t understand why in the struct the char pName is being marked as ‘*’ whereas in the char[100] it’s not.

Any brief explanation please?

Thank you in advance

EDIT: After emending the code with some of below:
THIS Is the INPUT NAME OF PRODUCT section;

char *nameCheck;
        nameCheck = "";
        fgets(nameCheck,sizeof nameCheck, stdin);

        checkProduct (nameCheck);

        int value = checkProduct (nameCheck);
        while (value == 1)
        {
            printf ("Product Already Exists!\n Enter another!\n");
            while (getchar() !='\n')
            {

            }
        }
        strcpy (p.pName, nameCheck);

this is the new checkName method

int checkProduct (char *nameCheck)
{
    product temp;
    strcpy (p.pName, nameCheck);

    rewind (pfp);
    while (fread(&temp,STRUCTSIZE,1,pfp)==1)
    {
        if (strcmp (temp.pName,p.pName) == 0)
        {
            return 1;
        }
    }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:02:10+00:00Added an answer on June 16, 2026 at 7:02 am
    int checkProduct (char nameCheck[100])
    

    Note that the type signature is a lie. The signature should be

    int checkProduct(char *nameCheck)
    

    since the argument the function expects and receives is a pointer to a char, or, to document it for the user that the argument should be a pointer to the first element of a 0-terminated char array

    int checkProduct(char nameCheck[])
    

    Arrays are never passed as arguments to functions, as function arguments, and in most circumstances [the exceptions are when the array is the operand of sizeof, _Alignof or the address operator &] are converted to pointers to the first element.

        {
            product temp;
            p.pName = nameCheck;
    

    Arrays are not assignable. The only time you can have an array name on the left of a = is initialisation at the point where the array is declared.

    You probably want

    strcpy(p.pName, nameCheck);
    

    there.

            rewind (pfp);
            while (fread(&temp,STRUCTSIZE,1,pfp)==1)
            {
                if (strcmp (temp.pName,p.pName))
    

    strcmp returns a negative value if the first argument is lexicographically smaller than the second, 0 if both arguments are equal, and a positive value if the first is lexicographically larger than the second.

    You probably want

    if (strcmp(temp.pName, p.pName) == 0)
    

    there.

        gets (nameCheck);
    

    Never use gets. It is extremely unsafe (and has been remoed from the language in the last standard, yay). Use

    fgets(nameCheck, sizeof nameCheck, stdin);
    

    but that stores the newline in the buffer if there is enough space, so you have to overwrite that with 0 if present.

    If you are on a POSIX system and don’t need to care about portability, you can use getline() to read in a line without storing the trailing newline.

        checkProduct (nameCheck);
    

    You check whether the product is known, but throw away the result. Store it in a variable.

        while (checkProduct == 1)
    

    checkProduct is a function. In almost all circumstances, a function designator is converted into a pointer, hence the warning about the comparison between a pointer and an integer. You meant to compare to the value of the call you should have stored above.

        {
            printf ("Product Already Exists!\n Enter another!\n");
            while (getchar() !='\n')
    

    You read in characters without storing them. So you will never change the contents of nameCheck, and then be trapped in an infinite loop.

            {
                continue;
            }
    

    If the only statement in a loop body is continue;, you should leave the body empty.

        }
        p.pName = nameCheck;
    

    Once again, you can’t assign to an array.


    Concerning the edit,

    char *nameCheck;
    nameCheck = "";
    fgets(nameCheck,sizeof nameCheck, stdin);
    

    you have changed nameCheck from an array to a pointer. That means that sizeof nameCheck now doesn’t give the number of chars you can store in the array, but the size of a pointer to char, which is independent of what it points to (usually 4 on 32-bit systems and 8 on 64-bit systems).

    And you let that pointer point to a string literal "", which is the reason for the crash. Attempting to modify string literals is undefined behaviour, and more often than not leads to a crash, since string literals are usually stored in a read-only segment of the memory nowadays.

    You should have left it at

    char nameCheck[100];
    fgets(nameCheck, sizeof nameCheck, stdin);
    

    and then you can use sizeof nameCheck to tell fgets how many characters it may read, or, alternatively, you could have a pointer and malloc some memory,

    #define NAME_LENGTH 100
    char *nameCheck = malloc(NAME_LENGTH);
    if (nameCheck == NULL) {
        // malloc failed, handle it if possible, or
        exit(EXIT_FAILURE);
    }
    fgets(nameCheck, NAME_LENGTH, stdin);
    

    Either way, after getting input, remove the newline if there is one:

    size_t len = strlen(nameCheck);
    if (len > 0 && nameCheck[len-1] == '\n') {
        nameCheck[len-1] = 0;
    }
    // Does windows also add a '\r' when reading from stdin?
    if (len > 1 && nameCheck[len-2] == '\r') {
        nameCheck[len-2] = 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have this current database which we need to replace some tables by anther
I have a database entry that I need to update. This method obviously doesn't
In mysql database i have this column called: Name: Date Type: datetime I have
I have this very simple login form with user and pass and I need
I have this database structure, SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; CREATE TABLE IF NOT EXISTS `announces` (
I have this enormous database : # # Dropping tables # DROP TABLE IF
So I have this game database, where I have several users with fields, id,
I have this old Access database (2000 format) which i want to convert to
I have this field from the database which is: DataType: Numeric (7, 3) So
I have this line: @users = database['users'].find(:all).limit(10) it returns this object: <Mongo::Cursor:0x8759a858 namespace='app-development.users' @selector=:all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.