为啥这个switch函数末尾一定要加这个 (getchar() != '\n'

#include<stdio.h>
#include<stdbool.h>
void show_menu(void);
int main(void) {
char selected;
float income = 0;
float rates = 0;
do {
show_menu();
printf("选择你的类型:\n");
switch (selected=getchar()) {//switch这里的输入值必须是%c对应selected,最后会默认有一个/n
case'1':
printf("你的类型为单身,税金计算方法为17850美元按15%%计,超过部分按28%%计\n");
printf("输入你的收入:");
scanf("%f", &income);
rates = (income <= 17850) ? income * 0.15 : (income - 17850.0) * 0.28 + 17850 * 0.15;
printf("你的税金为%.2f\n", rates);
break;
case'2':
printf("你的类型为户主,税金计算方法为23900美元按15%%计,超过部分按28%%计\n");
printf("输入你的收入:");
scanf("%f", &income);
rates = (income <= 23900) ? income * 0.15 : (income - 23900.0) * 0.28 + 23900 * 0.15;
printf("你的税金为%.2f\n", rates);
break;
case'3':
printf("你的类型为已婚,共有,税金计算方法为29750美元按15%%计,超过部分按28%%计\n");
printf("输入你的收入:");
scanf("%f", &income);
rates = (income <= 29750) ? income * 0.15 : (income - 29750.0) * 0.28 + 29750 * 0.15;
printf("你的税金为%.2f\n", rates);
break;
case'4':
printf("你的类型为已婚,离异,税金计算方法为14875美元按15%%计,超过部分按28%%计\n");
printf("输入你的收入:");
scanf("%f", &income);
rates = (income <= 14875) ? income * 0.15 : (income - 14875.0) * 0.28 + 14875 * 0.15;
printf("你的税金为%.2f\n", rates);
break;
case'5':
break;
default:
printf("WRong type.Please retry.\n");
}
while (getchar() != '\n')//这里为啥一定要加这个函数啊,不加的话每次都会因为换行符默认进入default*
continue;
} while (selected != '5');
printf("\nDone!\n");
return 0;
}
void show_menu(void) {
printf(" |类型**********|税金****************************\n");
printf("(1)|单身 |17850美元按15%计,超过部分按28%计\n");
printf("(2)|户主 |23900美元按15%计,超过部分按28%计\n");
printf("(3)|已婚,共有 |29750美元按15%计,超过部分按28%计\n");
printf("(4)|已婚,离异 |14875美元按15%计,超过部分按28%计\n");
}

输入按回车后,回车还在输入流缓存里,通过这个把前面输入的回车符清理掉

你看逻辑啊,假如你输入一个a,再按回车前,
你程序就会判断非法,
提示printf("WRong type.Please retry.\n");
如果你不等待回车或者不要这一行,就会继续循环,转到switch (selected=getchar()) {//switch这里的输入值必须是%c对应selected,最后会默认有一个/n
你不按回车却再按个1理论上是可以跑通,但用户体验很不好