Because strcmp() will return a negative integer in this case.
So change this:
if (strcmp(one, two) == 1) {
to this:
if (strcmp(one, two) != 0) {
to take into account all the cases that the strings differ.
Notice that you could have spotted that yourself by either reading the ref or by printing what the functions returns, like this:
printf("%d\n", strcmp(one, two));
// prints -23
Answer from gsamaras on Stack OverflowBecause strcmp() will return a negative integer in this case.
So change this:
if (strcmp(one, two) == 1) {
to this:
if (strcmp(one, two) != 0) {
to take into account all the cases that the strings differ.
Notice that you could have spotted that yourself by either reading the ref or by printing what the functions returns, like this:
printf("%d\n", strcmp(one, two));
// prints -23
The correct behaviour is:
if (strcmp(one, two) != 0) {
printf("hello world\n");
}
Actually, this function returns the difference between two strings:
- < 0: the first character that does not match has a lower value in ptr1 than in ptr2.
- 0: the contents of both strings are equal
- > 0: the first character that does not match has a greater value in ptr1 than in ptr2.
This is an example of how strcmp could be implemented
Videos
I have this little program
printf("Welcome to the command line interpreter \n");
printf("Enter your command: \n");
fgets(command,80,stdin);
if(strcmp(command,"load"))
load();
else if(strcmp(command,"execute"))
execute();
else
printf("nothing happened\n");
printf("your command is:%s\n",command);
return 0;}
I only get the comparison of the first if, the rest is ignore even the else and I have no idea why. If I type load I do get the first function to work, but if I put something else I still get the result of the first if.
Because the value stored by fgets() contains a trailing '\n'.
Try this
int stop = 0;
while (stop == 0)
{
fgets(input, MAX, stdin);
printf("%s", input);
if (strcmp(input, "1\n") == 0)
add();
else if (strcmp(input, "2\n") == 0)
delete();
else if (strcmp(input, "3\n") == 0)
view();
else if (strcmp(input, "4\n") == 0)
stop = 1;
else
printf("Invalid Input!\n");
}
did it work?
So you need to remove it from input or adding to the comparison string.
In addition to what @iharob said, I would suggest using strncmp to check your inputs. That function lets you explicitly specify how many characters to compare. See here for a function definition.
int stop = 0;
while (stop == 0)
{
fgets(input, MAX, stdin);
printf("%s", input);
if (strncmp(input, "1", 1) == 0)
add();
else if (strncmp(input, "2", 1) == 0)
delete();
else if (strncmp(input, "3", 1) == 0)
view();
else if (strncmp(input, "4", 1) == 0)
stop = 1;
else
printf("Invalid Input!\n");
}
fgets will return a string which ends with a newline (\n). From its documentation
Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.
You could either test for a trailing newline and strip it off
fgets(input, 20, stdin);
size_t len = strlen(input);
if (input[len-1] == '\n') {
input[len-1] = '\0';
}
or read user input using scanf instead.
scanf("%19s", input);
As an aside
const char gasCheck[4] = "gas";
const char electricityCheck[13] = "electricity";
could be declared slightly more easily and safely as
const char *gasCheck = "gas";
const char *electricityCheck = "electricity";
(This form saves copying the string literals into stack variables. More importantly, it removes a potential source of bugs if you hard-code too small a length for the arrays.)
fgets() read characters from stdin until a newline is seen or an EOF, if newline was seen, it would be stored in the array.Anyway, fgets() appends a null character to the array.
so if you want end your input by newline, my minor alteration is here, change this
const char gasCheck[4] = "gas";
const char electricityCheck[13] = "electricity";
to
const char gasCheck[5] = "gas\n";
const char electricityCheck[14] = "electricity\n";
strcmp returns 0 if the strings compare equivalently.
Try negating your if statement, or checking for 0 explicitly.
if (!strcmp(...))
if (strcmp(...) == 0)
strcmp Reference
Return value
- Negative value if lhs appears before rhs in lexicographical order.
- Zero if lhs and rhs compare equal.
- Positive value if lhs appears after rhs in lexicographical order.
strcmp returns 0 when both strings are equal.
It should be
if (strcmp(inputUnit, "in") == 0) {
returnValue = inputValue * 2.54;
printf("%.2f %s = %.4f cm\n", inputValue, inputUnit, returnValue);
}
See
man strcmp
#include <string.h> int strcmp(const char *s1, const char *s2);DESCRIPTION
The
strcmp()function compares the two stringss1ands2. It returns an integer less than, equal to, or greater than zero ifs1is found, respectively, to be less than, to match, or be greater thans2.
this is invlaid code , read 'man strchr' you will see it looks for a character in a string. The compiler is surely complaining big time at you
strchr(day, "Sunday") == 0
you need
strcmp(day, "Sunday") == 0
strchr() is used to search for a character in a string. You should use strcmp() to compare two strings.
if (!strcmp(day, "Saturday") && ...) {
// do stuff ...
}
There is also stricmp() if you want case-insensitive comparison.
if (!stricmp(day, "Saturday") && ...) { // SATURDAY is also matched
// do stuff ...
}
One thing to keep in mind, using scanf() to read user input is dangerous. You should consider using fgets() along with sscanf() for that purpose.
char food[20];
int numFood;
printf("Enter food: ");
if (!fgets(food, sizeof food, stdin)) {
// Handle failure...
}
size_t newlinePos = strcspn(food, "\n"); // fgets() reads the newline delimiter
food[newlinePos] = '\0'; // so make sure to replace it with a null-terminator
...
char tmp[20];
printf("Enter the number of %s you can eat: ", food);
if (!fgets(tmp, sizeof tmp, stdin)) {
// Handle failure...
}
newlinePos = strcspn(tmp, "\n");
tmp[newlinePos] = '\0';
if (sscanf(tmp, "%d", &numFood) != 1) {
// Wrong user input
}
// The rest of your code...
in the following snippet: https://pastebin.com/YTEJkbZ3
in the if() statement, it uses !strcmp and not strcmp. I thought strcmp returned true, or 0 if the two values were the same. But it's not? can someone explain this to me?