How Can I replace this type "int" by"String" in c programming? -
this question has answer here:
- how check if value matches string 3 answers
i'm using dev c++ . silly program ask correct key show text. program working type "int" (just numbers):
#include <stdio.h> #include<conio.h> main() { int key; printf("this program show mensage if correct key \n"); printf("write key:\n"); scanf("%i",&key); if(key==1234) { printf("welcome stackoverflow \n"); } else { printf("you keep going slept \n"); } getch(); return 0; }
but how can replace strings example: sesame, house , on.
i've tried matrix:
char key[];
however error.
sincerily nin.
update:
i new program :
#include <stdio.h> #include<conio.h> main() { char key[7]; /*replace int char*/ printf("this program show mensage if correct key \n"); printf("write key:\n"); scanf("%c",&key); if(key=="sesame") /*numbers string*/ { printf("welcome stackoverflow \n"); } else { printf("you keep going slept \n"); } getch(); return 0; }
however though fix correct key ("sesame") "you keep going slept"
problem 1:
scanf("%c",&key);
is not right. read 1 character. need use
scanf("%6s", key);
problem 2:
if(key=="sesame")
is not proper way compare 2 strings. compare 2 pointers , evaluate false. need use:
if( strcmp(key, "sesame") == 0 )
Comments
Post a Comment