shape
에 입력 한 내용에 관계없이 프로그램을 실행할 때 마지막 else 문이 실행됩니다.C char가 if..else에서 작동하지 않습니다.
#include <stdio.h>
int main(void)
{
char shape = 'a';
printf("What shape do you want?\nEnter 's' for a Square, 'b' for a Box, 't' for a Triangle\n");
while (shape != 's' && shape != 'b' && shape != 't')
{
scanf_s(" %c", &shape);
if (shape == 's')
{
printf("You entered %c", shape);
}
else if (shape == 'b')
{
printf("You entered %c", shape);
}
else if (shape == 't')
{
printf("you entered %c", shape);
}
else
{
printf("Please enter 's' 'b' or 't'");
}
}
return 0;
}
형식 문자열의 선행 공백을 제거하십시오. scanf_s는 단일 문자를 읽는 데 적합하지 않습니다. getchar은 아마도 더 나은 선택 일 것입니다. – Hogan
'C '는 모르지만'scanf_s ("% c", & shape);는 입력을 캡처하지 않습니다. BTW, 당신의'동안 '항상 사실이 될 것입니다. – yizzlez
@ user3525735 : [게시물 및 의견에 대한 혐오와 관련된 어떠한 것도 피하십시오.] (http://meta.stackexchange.com/a/3021/242800) (감사합니다. 제발 ...). – user2284570