프로그래밍

 3198, 1/160 회원가입  로그인  
   nsh009
   문자열을 사용한 if 문

http://www.hackerschool.org/HS_Boards/zboard.php?AllArticle=true&no=6315 [복사]


              

               int x,y;
        char cal_operator;

        char inexit[5];
        ////// 계산기변수 선언 윗부분.//////
        printf("\n Welcome to the calculator. \n");

        while(1)
        {
                printf("If you want to exit this function input 'exit'. \n");
                printf("Continue: press any key and enter. \n");
                fflush(stdin);
                scanf("%s", &inexit);
                /////이부분
                                      if(inexit=="exit\0")
                        break;

                printf("Input x value: ");
                scanf("%d", &x);
                printf("Input y value: ");
                scanf("%d", &y);
                fflush(stdin);
                printf("Select the operator, '+' '-' '*' '/' '%' \n");
                scanf("%c", &cal_operator);
                             //  이하 코드 //

코드중 일부 입니다. exit 값을 받으면 while 문을 종료 하는 건데.. if 부분에서 inexit 변수와 exit 가 같으면 빠져 나가야 되는데 빠져 나가지 않고 계속 실행이 되네요!

  Hit : 10761     Date : 2013/03/13 02:55



    
gijun0102 C언어에서는 문자열이 == 으로 비교 안됩니다. C++에서는 string 클래스를 이용하면 되는데 C언어를 쓰실려면 strcmp() 함수를 써야 됩니다 2013/03/15  
6월 제가 알고있는게 맞는지 모르겠지만 \0 굳이 안주셔도 되고 " " 문자열이 끝날 때 NULL 이 들어가는걸로 알고있습니다. 2013/03/15  
youngim0405 아직 string 함수를 안배우신것같은데 zㅣzㅕㄴ 님께서 말씀하신데로 C언어에서는 ==로 문자열이 비교가 안됩니다...Java나 C++는 되는데 말이죠. 그래서 문자열을 비교하시고 싶으실때는 #include <string.h>를 호출하셔서 string 함수를 사용할수 있겠끔 적용햊주신다음 strcmp(char const* a, char const*b)를 사용하셔서 코딩해주시면 되겠습니다.

strcmp는 두 문자열이 같을때 0을 리턴하고 다를때는 다른 값만큼 (예: a="a", b="c" strcmp=2) 출력되기 때문에 알아서 코딩해주시면 되겠에요. 그런데 strcmp가 생각만큼 쉽...지는 앟을껍니다.
2013/03/29