帮我看下这个c程序哪里不对

来源:百度知道 编辑:UC知道 时间:2024/06/05 22:14:34
#include <stdio.h>
#include"math.h"
void main()
{char letter;
printf("Please input the first number\n");
scanf("%c",&letter);
{switch(letter)
{case('M'):printf("Monday\n");break;
case('T'):printf("please input the second number\n");
if((letter=getchar())=='u')
printf("Tuesday\n");
else if((letter=getchar())=='h')
printf("Thursday\n");
else
printf("error\n");
break;
case('W'):printf("Wednesday\n");break;
case('F'):printf("Friday\n");break;
case('S'):printf("Please input the second number\n");
if ((letter=getchar())=='a')
printf("Saturday\n");
else if((letter=getchar())=='u')
printf("Sunday\n");
else

这是因为scanf("%c",&letter); 在你输入完毕后你又输入了一个回车,这个回车键被存入到了系统区,而getchar()是直接从缓冲区读取数据,所以第一次读的是这个回车,而不是你输入的a,不光这个地方,在你输入T后再输入u,同样得不到Tuesday,解决方法:
在scanf("%c",&letter); 之后再加一句getchar();

另外程序中还有一处错误,
if ((letter=getchar())=='a') /*假设输入u*/
printf("Saturday\n");
else if((letter=getchar())=='u') /*此时letter中已经是u了,如果再用getchar赋值的话,就会要你重新读入一个数据,而不是用前面输入的u,可改为else if(letter=='u'),同样再case('T')中也有这个问题*/
printf("Sunday\n");
else
printf("error\n");
break;

加点分哦