scanf_s와 switch-case문 오류

조회수 322회
#include <stdio.h>

int main(void)
{
   int num1, num2;
   char arithmetic;

   printf("숫자 2개와 연산자를 입력하세요.\n\n");

   printf("연산자는 아래와 같습니다.\n");
   printf("더하기:+, 빼기:-, 곱하기:*, 나누기:/\n");
   printf("입력 순서: 정수1 연산자 정수2\n\n");
   scanf_s("%d %c %d", &num1, &arithmetic, &num2, sizeof(num1), sizeof(arithmetic), sizeof(num2));

   switch(arithmetic)
   {
    case'+':
      printf("%d %c %d = %d\n", num1, arithmetic, num2, num1 +num2);
      break;
    case'-':
      printf("%d %c %d = %d\n", num1, arithmetic, num2, num1 -num2);
      break;
    case'*':
      printf("%d %c %d = %d\n", num1, arithmetic, num2, num1 *num2);
      break;
    case'/':
      printf("%d %c %d = %d\n", num1, arithmetic, num2, (float)num1 /num2);
      break;

   default:
      printf("연산자를 잘못 입력했습니다.\n");
      break;
   }

   return 0;
}

로 짰는데 입력 받고 나서 까지는 되는데 뒤에 switch-case문이 출력이 안되네요... 어디가 문제가 있을까요?

1 답변

  • 틀린 곳 1

    scanf_s("%d %c %d", &num1, &arithmetic, &num2, sizeof(num1), sizeof(arithmetic), sizeof(num2));
    

    아래와 같이 해야 합니다.

    scanf_s("%d %c %d", &num1, &arithmetic, sizeof(arithmetic), &num2);
    

    틀린 곳 2

    printf("%d %c %d = %d\n", num1, arithmetic, num2, (float)num1 / num2);
    

    아래와 같이 해야 합니다.

    printf("%d %c %d = %f\n", num1, arithmetic, num2, (float)num1 / num2);
    
    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)