I am trying to get a program to let a user enter a word or character, store it, and then print it until the user types it again, exiting the program. My code looks like this:
#include <stdio.h>
int main()
{
char input[40];
char check[40];
int i = 0;
printf("Hello!\nPlease enter a word or character:\n");
gets(input); /* obsolete function: do not use!! */
printf("I will now repeat this until you type it back to me.\n");
while (check != input)
{
printf("%s\n", input);
gets(check); /* obsolete function: do not use!! */
}
printf("Good bye!");
return 0;
}
The problem is that I keep getting the printing of the input string, even when the input by the user (check) matches the original (input). Am I comparing the two incorrectly?
gets( )was removed from the standard. Usefgets( )instead.strcmp()return zero when its inputs are equal explains how to compare strings for equality, inequality, less than, greater than, less than or equal, and greater than or equal. Not all string comparisons are for equality. Case sensitive comparisons are different again; other special comparisons (dictionary order, for example) require more specialized comparators, and there are regexes for still more complex comparisons.gets()is a no-go. It has also been removed from the standard since C11 -> Please read Why is the gets function so dangerous that it should not be used?