switch 输出报错!
#include <stdio.h>main()
{
char letter;
printf("please in put the first letter\n");
while (letter=getchar()!='Y')
{
switch (letter)
{
case 'M':printf("Today is Monday\n");break;
case 'T':printf("please input the second letter\n");
if (getchar()=='u')
printf("Today is Tuesday\n");
else if (getchar()=='h')
printf("Today is Thursday\n");
else
printf("data error1\n");
break;
case 'W':printf("Today is Wednesday");break;
case 'F':printf("Today is Friday");break;
case 'S':printf("please input the second letter\n");
if (getchar()=='a')
printf("Today is Satday\n");
else if (getchar()=='h')
printf("Today is Sunday\n");
else
printf("data error2\n");
break;
default:printf("data error3\n");
}
}
}
这各程序执行的时候总是输出 data error3,请帮忙查找原因,谢谢!
----------------解决方案--------------------------------------------------------
LZ注意区分输入字母的大小写,你输的是小写字母吧
----------------解决方案--------------------------------------------------------
以下是引用xs_200在2007-11-16 16:06:55的发言:
#include <stdio.h>
void main()
{
char letter;
printf("please in put the first letter\n");
while (letter=getchar()!='Y') //while ((letter=getchar())!='/ n')
{
switch (letter)
{
case 'M': printf("Today is Monday\n");break;
case 'T': printf("please input the second letter\n");
if (getchar()=='u') //if ((getchar())=='u')
printf("Today is Tuesday\n");
else if (getchar()=='h') //else if ((getchar())=='h')
printf("Today is Thursday\n");
else
printf("data error1\n");
break;
case 'W': printf("Today is Wednesday");break;
case 'F': printf("Today is Friday");break;
case 'S': printf("please input the second letter\n");
if (getchar()=='a') // if ((getchar())=='a')
printf("Today is Satday\n");
else if (getchar()=='h') // else if ((getchar())=='h')
printf("Today is Sunday\n");
else
printf("data error2\n");
break;
default: printf("data error3\n");
}
}
}
这各程序执行的时候总是输出 data error3,请帮忙查找原因,谢谢!
#include <stdio.h>
void main()
{
char letter;
printf("please in put the first letter\n");
while (letter=getchar()!='Y') //while ((letter=getchar())!='/ n')
{
switch (letter)
{
case 'M': printf("Today is Monday\n");break;
case 'T': printf("please input the second letter\n");
if (getchar()=='u') //if ((getchar())=='u')
printf("Today is Tuesday\n");
else if (getchar()=='h') //else if ((getchar())=='h')
printf("Today is Thursday\n");
else
printf("data error1\n");
break;
case 'W': printf("Today is Wednesday");break;
case 'F': printf("Today is Friday");break;
case 'S': printf("please input the second letter\n");
if (getchar()=='a') // if ((getchar())=='a')
printf("Today is Satday\n");
else if (getchar()=='h') // else if ((getchar())=='h')
printf("Today is Sunday\n");
else
printf("data error2\n");
break;
default: printf("data error3\n");
}
}
}
这各程序执行的时候总是输出 data error3,请帮忙查找原因,谢谢!
[此贴子已经被作者于2007-11-16 16:43:28编辑过]
----------------解决方案--------------------------------------------------------
再次提示编码风格问题,开始我没发现问题,是我习惯拿到一个程序后改成自己喜欢的风格来看
事实上已经帮你改好了,差点没看出来,以为你输错大小写了
----------------解决方案--------------------------------------------------------
请教:调试时,发现敲入一个字符后,出现两次data error3。说明回车符也被当作字符处理了,有没解决方法
----------------解决方案--------------------------------------------------------
以下是引用hczsea在2007-11-16 17:16:06的发言:
请教:调试时,发现敲入一个字符后,出现两次data error3。说明回车符也被当作字符处理了,有没解决方法
请教:调试时,发现敲入一个字符后,出现两次data error3。说明回车符也被当作字符处理了,有没解决方法
有。
----------------解决方案--------------------------------------------------------
while (letter=getchar()!='Y') 应该这个才是错因。由于!=运算等级高于 =
所以,先判断了 getchar() != 'Y' ,如果输入的不是‘Y’,那么返回真,也就是1给letter 。所以一直有错。
----------------解决方案--------------------------------------------------------
以下是引用cosdos在2007-11-16 18:02:15的发言:
有。
回答得很精辟啊
----------------解决方案--------------------------------------------------------
回复:(chmlqw)以下是引用cosdos在2007-11-16 18:02...
// 没仔细看楼主的代码所以只能说有。#include <stdio.h>
int main(void)
{
int letter; /* 建议使用 int 因为 getchar() 返回 int 类型 */
int ch;
printf("please in put the first letter\n");
while((letter = getchar()) !='Y')
{
if(letter != '\n')
while(getchar() != '\n');
switch(letter)
{
case 'M':
printf("Today is Monday\n");
break;
case 'T':
printf("please input the second letter\n");
if((ch = getchar()) == 'u')
{
if(ch != '\n')
while(getchar() != '\n');
printf("Today is Tuesday\n");
}
else if((ch = getchar()) == 'h')
{
if(ch != '\n')
while(getchar() != '\n');
printf("Today is Thursday\n");
}
else
printf("data error1\n");
break;
case 'W':
printf("Today is Wednesday");
break;
case 'F':
printf("Today is Friday");
break;
case 'S':
printf("please input the second letter\n");
if((ch = getchar()) == 'a')
{
if(ch != '\n')
while(getchar() != '\n');
printf("Today is Satday\n");
}
else if((ch = getchar()) == 'h')
{
if(ch != '\n')
while(getchar() != '\n');
printf("Today is Sunday\n");
}
else
printf("data error2\n");
break;
default:
printf("data error3\n");
}
}
return 0;
}
/* 这个程序是干什么的? */
[此贴子已经被作者于2007-11-16 18:20:06编辑过]
----------------解决方案--------------------------------------------------------
这程序风格真差啊……………………
----------------解决方案--------------------------------------------------------