#include<stdio.h> int main() { int i, j; int input;
fflush(stdin); do {
printf("Input number(1~99) :> "); scanf_s(" %d", &input);
if (input < 0) { printf("Wrong input numbern"); continue; } if (input>100) { printf("Wrong input numbern"); continue; } if (input % 2 == 1) { for (i = 0; i<input; i += 2) { for (j = 0; j<i / 2; j++) { printf(" "); } for (j = input; j>i; j--) { printf("*"); } printf("n"); } for (i = input - 2; i>0; i -= 2) { for (j = i / 2; j>0; j--) { printf(" "); } for (j = i; j <= input; j++) { printf("*"); } printf("n"); } break; } else if (input == 0) { printf("Wrong input numbern"); continue; } else printf("Wrong input numbern"); continue; fflush(stdin);
} while (1); return 0; } 인데 문자나 짝수를 입력하면 오류가 나와야하는데 각각 입력하거나 첫자로 입력하면 오류가 뜸 ex) 2ddddd , d24e34 |
scanf_s 볼때마다 암걸릴꺼 같다
ㅁㄴㅇㅁㄴ
%d로 받으면 scanf가 자동으로 문자 걸러주지 않나요.
디버깅 ㄱ
5d처럼 입력하면 원래 stdin에 문자가 남아있으니 무한루프 돌아야 되는데 fflush하니까 숫자만 받고 다음으로 넘어가겠네요
ㄴ 5d 이렇게 쳤을때 오류나게하려면 혹시 어떻게해야하는지요?
굳이 그렇게까지 해야할 이유가 있는지 모르겠지만 문자열로 받아서 숫자가 아닌 문자가 들어있으면 걸러내고 다시 숫자로 변환해주면 됩니다