想问一下大佬们,这个程序哪里出了问题,导致我最后的结果不对,我感觉从Y,N输入就有问题,可我没找出来

#include <stdio.h>
int main()
{
	char sex,sports,diet;
	float faHeight,moHeight,chilHeight;
		printf("请输入您的性别,女性输F,男性输M:");
			sex=getchar();
		printf("请输入您父亲的身高(厘米)faHeight=,母亲的身高(厘米)moHeight=",faHeight,moHeight);
			scanf("%f %f",&faHeight,&moHeight);
		printf("您是否喜欢体育锻炼,“是”请输入Y,“不是”请输入N,%c\n");
			sports=getchar();
		printf("您的饮食习惯是否健康,“是”请输入Y,“不是”请输入N,%c\n");
			diet=getchar();
				if(sex=='M')
				{
					if(sports=='Y'&&diet=='Y')
						{chilHeight=(faHeight+moHeight)*0.54*(1+0.02+0.015);
						}
					else if(sports=='Y'&&diet=='N')
						{chilHeight=(faHeight+moHeight)*0.54*(1+0.02);
						}
					else if(sports=='N'&&diet=='Y')
						{chilHeight=(faHeight+moHeight)*0.54*(1+0.015);
						}
					else if(sports=='N'&&diet=='N')
						{chilHeight=(faHeight+moHeight)*0.54;
						}
					printf("您未来的身高chilHeight=%f\n",chilHeight);
				}
				else if(sex=='F')
				{
					if(sports=='Y'&&diet=='Y')
						{chilHeight=((faHeight*0.923+moHeight)/2)*(1+0.02+0.015);
						}
					else if(sports=='Y'&&diet=='N')
						{chilHeight=((faHeight*0.923+moHeight)/2)*(1+0.02);
						}
					else if(sports=='N'&&diet=='Y')
						{chilHeight=((faHeight*0.923+moHeight)/2)*(1+0.015);
						}
					else if(sports=='N'&&diet=='N')
						{chilHeight=((faHeight*0.923+moHeight)/2)*(1+0.02+0.015);
						}
					printf("您未来的身高chilHeight=%f\n",chilHeight);
				}
		return 0;
}

#include <stdio.h>
#define N 2
int main()
{
	char sex[N],sports[N],diet[N];
	float faHeight,moHeight,chilHeight;
		printf("请输入您的性别,女性输F,男性输M:");
			scanf("%s",sex); 
		printf("请输入您父亲的身高(厘米)faHeight=,母亲的身高(厘米)moHeight=");
			scanf("%f %f",&faHeight,&moHeight);
		printf("您是否喜欢体育锻炼,“是”请输入Y,“不是”请输入N\n");
			scanf("%s",sports); 
		printf("您的饮食习惯是否健康,“是”请输入Y,“不是”请输入N\n");
			scanf("%s",diet); 
				if(sex[0]=='M')
				{
					if(sports[0]=='Y'&&diet[0]=='Y')
						{chilHeight=(faHeight+moHeight)*0.54*(1+0.02+0.015);
						}
					else if(sports[0]=='Y'&&diet[0]=='N')
						{chilHeight=(faHeight+moHeight)*0.54*(1+0.02);
						}
					else if(sports[0]=='N'&&diet[0]=='Y')
						{chilHeight=(faHeight+moHeight)*0.54*(1+0.015);
						}
					else if(sports[0]=='N'&&diet[0]=='N')
						{chilHeight=(faHeight+moHeight)*0.54;
						}
					printf("您未来的身高chilHeight=%f\n",chilHeight);
				}
				else if(sex[0]=='F')
				{
					if(sports[0]=='Y'&&diet[0]=='Y')
						{chilHeight=((faHeight*0.923+moHeight)/2)*(1+0.02+0.015);
						}
					else if(sports[0]=='Y'&&diet[0]=='N')
						{chilHeight=((faHeight*0.923+moHeight)/2)*(1+0.02);
						}
					else if(sports[0]=='N'&&diet[0]=='Y')
						{chilHeight=((faHeight*0.923+moHeight)/2)*(1+0.015);
						}
					else if(sports[0]=='N'&&diet[0]=='N')
						{chilHeight=((faHeight*0.923+moHeight)/2)*(1+0.02+0.015);
						}
					printf("您未来的身高chilHeight=%f\n",chilHeight);
				}
		return 0;
}

		char sex, sports, diet;
		float faHeight, moHeight, chilHeight;
		printf("请输入您的性别,女性输F,男性输M:");
		sex = getchar();
		printf("请输入您父亲的身高(厘米)faHeight=,母亲的身高(厘米)moHeight=");
		scanf("%f %f", &faHeight, &moHeight);
		printf("您是否喜欢体育锻炼,“是”请输入Y,“不是”请输入N\n");
		fflush(stdin);
		sports = getchar();
		printf("您的饮食习惯是否健康,“是”请输入Y,“不是”请输入N\n");
		fflush(stdin);
		diet = getchar();

 

问题在于getchar()函数。每次输入完一组参数值之后都需要回车(换行符)才能输入下一个参数,而getchar()函数每次调用只会读入一个字符,此时不会读入换行符,而会留在输入缓冲区等待下一个函数读取。之所以输入性别后再输入身高没有问题,是因为scanf()函数能自动忽略换行符。

两种解决方法:

1. 全部改用scanf()函数读参数

2. 在每次用getchar()函数读完参数后再一次调用getchar()函数读取换行符

键盘输入字符时存在的一些问题:https://blog.csdn.net/qq_41071068/article/details/90300324

C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html
C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html