我是靠谱客的博主 自信红牛,这篇文章主要介绍8.2 习题编写一个程序,在遇到EOF之前,把输入作为字符流读取,程序需要打印每个输入的字符及其相应的ASCLL十进制..........,现在分享给大家,希望可以做个参考。

#include <stdio.h>
int main(void)
{
char ch;
int number = 0;
printf("please enter the word you want to print: ");
while((ch = getchar() != EOF)) //错误点!
{
switch (ch)
{
case 'n':
printf("\n");
printf("%d
",ch);
break;
case 't':
printf("\t");
printf("%d
",ch);
break;
default:
if(ch < ' ')
{
putchar('^');
putchar(ch+64);
printf(":%d ", ch);
}
else
{
putchar(ch);
printf(":%d ", ch);
break;
}
}
number++;
if(number %10 == 0)
putchar('n');
}
return 0;
}



一直没有找到错误,发现程序只会进入default 中的第一个if ,并且ch的值一直是1,最后发现 是在while 循环里面的括号错了,(应该是 (ch = getchar()) != EOF), 按照我这样写的话, 由于运算优先的顺序,会先执行 getchar()!= EOF, 那么这样执行的结果 要么为1 要么为0 ,这样ch的值就在这两个值中。从而导致了错误。!!!!!!!下次一定要注意!,,,,并且switch 不能用在于浮点型中。

最后

以上就是自信红牛最近收集整理的关于8.2 习题编写一个程序,在遇到EOF之前,把输入作为字符流读取,程序需要打印每个输入的字符及其相应的ASCLL十进制..........的全部内容,更多相关8.2内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部