C Primer Plus汇总 (二)错误笔记2

特别注意可恶的\n啦,为什么最后ch的值会变?求助!!!

可恶可恶啊http://tieba.baidu.com/p/1429836706

include<stdio.h>
#include<stdbool.h>
int main(void)
{
 int w=1;
 float s;
 char ch;
 bool is,z; while(w)
 {
 for(is=true;is==true;)
 {
 z=true;
 printf("***************\n");
 printf("请输入下列字母\n");
 printf("a)朝鲜蓟 1.25$/磅\n");
 printf("b)甜菜 0.65$/磅\n");
 printf("c)胡萝卜 0.89$/磅\n");
 printf("q)退出\n");
 while((ch=getchar())&&z==true)
 {
 switch(ch)
 {
 case 'a':s=1.25;z=is=false;break;
 case 'b':s=0.65;z=is=false;break;
 case 'c':s=0.89;z=is=false;break;
 case 'q':z=s=false;break;
 default:z=false;printf("DONE,请重新输入\n");break;
 }
 }
 }
 //if(ch=='a')
 // printf("q");
 printf("1111111111%c",ch);
 }
 printf("循环结束");
return 0;
}

程序能正常输出1111111,DONE也能输出 可是去掉注释符//ch的值却不能显示

 

我错了,果然是\n

include<stdio.h>
#include<stdbool.h>
int main(void)
{
 int w=1;
 float s;
 char ch;
 bool is,z; while(w)
 {
 for(is=true;is==true;)
 {
 z=true;
 printf("***************\n");
 printf("请输入下列字母\n");
 printf("a)朝鲜蓟 1.25$/磅\n");
 printf("b)甜菜 0.65$/磅\n");
 printf("c)胡萝卜 0.89$/磅\n");
 printf("q)退出\n");
 while(z==true&&(ch=getchar()))
 {
 if(ch!='\n')
 {
 switch(ch)
 {
 case 'a':s=1.25;z=is=false;break;
 case 'b':s=0.65;z=is=false;break;
 case 'c':s=0.89;z=is=false;break;
 case 'q':z=s=false;break;
 default:z=false;printf("DONE,请重新输入\n");break;
 }
 printf("1%c",ch);
 }
 printf("2%c",ch);
 }
 printf("3%c",ch);
 }
 //if(ch=='a')
 // printf("q");
 printf("4%c",ch);
 }
 printf("循环结束");
return 0;
}

被除数为0的悲剧

http://tieba.baidu.com/p/1441525681

include<stdio.h>
#include<ctype.h>
int put(char x,int y,int z);
int main(void)
{
 int a,b;
 char ch;
 a=b=0;
 while((ch=getchar())!='#')
 {
 put(ch,a,b);
 }
 printf("共%d个字母,%d个单词,平均每个单词%d个字母.\n",a,b,a/b);
 return 0;
}
int put(char x,int y,int z)
{
 if(isalpha(x))
 y++;
 else if(isblank(x))
 z++;
 return 0;
}

啊啊啊,我又2了
除以0了

转载于:https://www.cnblogs.com/zero5/archive/2012/07/21/2602548.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值