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

来源:互联网 发布:linux mv命令详解 编辑:程序博客网 时间:2024/05/15 04:04

特别注意可恶的\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了

0 0
原创粉丝点击