![]() |
Use strcmp to check 2 values are not equal
Hi, I am writing a program which is to contain a list of library books. My problem is that the program is not too allow 2 ISBN numbers the same to be entered. I have used strcmp to check that the ISBN numbers are not the same, but I cannot get this to work, correctly. At the moment the program allows me to enter one ISBN then jumps to say ISBN already entered. Can anyone see where I am going wrong with my code. Thanks.
Code:
#include <stdio.h> |
Re: Use strcmp to check 2 values are not equal
Code:
strcmp (books.ISBN,books.ISBN);Usually you would test the return value, if it's zero the strings are equal, and do something accordingly, e.g. Code:
if (!strcmp(str1,str2))Code:
strcmp (books.ISBN,books.ISBN); |
Re: Use strcmp to check 2 values are not equal
I'm a beginner at this and I thought that the first strcmp would let the program continue as normal as the ISBN does not equal the ISBN and that the second strcmp would let the program know that the user has to be asked for a second ISBN to be entered as the ISBN is already on file.
|
Re: Use strcmp to check 2 values are not equal
> "...would let the program know..."
Ah. No, the program doesn't "know" anything. If you call a function and do nothing with the return value, then the return value is lost. The program knows nothing that you don't explicitly tell it, and if you don't explicitly tell it to do something about the return value then it'll do nothing. There is absolutely no intelligence there, and the program doesn't know what you mean. Assuming you're referring to the strcmp before the while (strcmp), the first is effectively a no-operation, because it doesn't modify the strings passed in and nothing happens to the return value. |
| All times are GMT +5.5. The time now is 22:07. |