-
problem with strcmp
hi there,
the code below is supposed to check the response typed from keyboard and perform an action based on that. but the if statements never evaluates to zero even when the correct response is entered. what am i doing wrong?
thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
int main()
{
char temp, ch;
int value;
printf("please confirm(y/n):");
scanf("%c", &temp);
strcpy(&ch,strlwr(&temp));
printf("%c\n", ch);
value = strcmp("y", &ch);
printf("comp result= %d\n ", value);
if ((strcmp("y", &ch)) == 0)
{
printf("confirmed\n");
exit(99);
}
else
if ((strcmp("n", &ch)) == 0)
{
printf("declined\n");
exit(99);
}
else
printf("invalid choice (y/n)\n");
return(0);
}
-
you are using a char type, instead of a char * type with memory allocated to the pointer... strcmp expects the char * type... if you are only requesting a single character [as indicated by your scanf usage], you don't need to use strlwr and strcmp to alter and compare it...