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;
}
Note that the type signature is a lie. The signature should be
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-terminatedchararrayArrays 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,_Alignofor the address operator&] are converted to pointers to the first element.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
there.
strcmpreturns 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
there.
Never use
gets. It is extremely unsafe (and has been remoed from the language in the last standard, yay). Usebut that stores the newline in the buffer if there is enough space, so you have to overwrite that with
0if 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.You check whether the product is known, but throw away the result. Store it in a variable.
checkProductis 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.You read in characters without storing them. So you will never change the contents of
nameCheck, and then be trapped in an infinite loop.If the only statement in a loop body is
continue;, you should leave the body empty.Once again, you can’t assign to an array.
Concerning the edit,
you have changed
nameCheckfrom an array to a pointer. That means thatsizeof nameChecknow doesn’t give the number ofchars you can store in the array, but the size of a pointer tochar, 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
and then you can use
sizeof nameCheckto tellfgetshow many characters it may read, or, alternatively, you could have a pointer andmallocsome memory,Either way, after getting input, remove the newline if there is one: