这个循环为什么执行到printf 后面的scanf部分不执行了?

u010423430 2013-05-15 07:48:55
#include<stdio.h>
#include<ctype.h>
int main(void)
{

float number_f;
float number_s;
char operation;
char choice_1;
do
{
printf("Please Edit the chaculation\n");
scanf("%1f%c%1f",&number_f,&operation,&number_s);
switch(operation)
{
case '+':
printf("You answer is %1f\n",number_f+number_s);
break;
case '-':
printf("You answer is %1f\n ",number_f-number_s);
break;
case '/':
if(number_s==0)
printf("the number s you put in is wrong\n");
else
printf("You answer is %1f \n",number_f/number_s);
break;
case '*':
printf("You answer is %1f\n" ,number_f*number_s);
break;
}
printf("DO you want anymore\n");
printf("Please Edit Y/N\n");
scanf("%c",&choice_1);

}
while(toupper(choice_1)=='y');

return 0;
}
...全文
383 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
derekrose 2013-05-17
  • 打赏
  • 举报
回复
感觉c语言的IO有可能让学生疯掉
u010423430 2013-05-17
  • 打赏
  • 举报
回复
引用 16 楼 AnYidan 的回复:
[quote=引用 12 楼 u010423430 的回复:] [quote=引用 5 楼 AnYidan 的回复:] scanf("%c",&choice_1); scanf(" %c",&choice_1);
感谢 但是为什么的一个scanf 键入的回车还会被 第二个scanf 读入呢? [/quote] 未匹配的 input 字符被留在 buffer[/quote] 搞定了 十分感谢
不顾-兼程 2013-05-16
  • 打赏
  • 举报
回复
这里的问题:scanf("%1f%c%1f",&number_f,&operation,&number_s); 当键入“+”或者“-”……,并且后,就进入到switch语句中去了。之后switch语句中的+-*/因为只获取到第一个参数number_f,而没有获取到第二个参数。这样就导致出错了。
liangkdm 2013-05-16
  • 打赏
  • 举报
回复
scanf语句能规定输入精度? 我记得是不可以的把
AnYidan 2013-05-16
  • 打赏
  • 举报
回复
引用 12 楼 u010423430 的回复:
[quote=引用 5 楼 AnYidan 的回复:] scanf("%c",&choice_1); scanf(" %c",&choice_1);
感谢 但是为什么的一个scanf 键入的回车还会被 第二个scanf 读入呢? [/quote] 未匹配的 input 字符被留在 buffer
赵4老师 2013-05-16
  • 打赏
  • 举报
回复
在每个最后不带\n的printf后面加fflush(stdout); 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。
u010423430 2013-05-15
  • 打赏
  • 举报
回复
引用 5 楼 AnYidan 的回复:
scanf("%c",&choice_1); scanf(" %c",&choice_1);
感谢 但是为什么的一个scanf 键入的回车还会被 第二个scanf 读入呢?
红金龙-时光 2013-05-15
  • 打赏
  • 举报
回复
非常感谢二楼, 百度给出的解释是 fflush(stdin)刷新输入缓冲区。 http://www.cnblogs.com/codingmylife/archive/2010/04/18/1714954.html 适合VC(VS),不适合GCC
红金龙-时光 2013-05-15
  • 打赏
  • 举报
回复
引用 5 楼 AnYidan 的回复:
scanf("%c",&choice_1); scanf(" %c",&choice_1);
想问下 这有什么区别?为什么会这样? 在那可以茶道资料 谢谢 这是 百度知道 给出的 解释 对么? 原因在于函数scanf把用户输入的回车和空格也当做一个有效的字符读入处理了(在你输入两个数按的那个回车被后面的scanf(" %c",&ch)读入),所以出错! 在%c前加空格就是为了存储回车或空格等特殊字符。
u010423430 2013-05-15
  • 打赏
  • 举报
回复
#include<stdio.h> #include<ctype.h> int main(void) { float number_f; float number_s; char operation; char choice_1; do { printf("Please Edit the chaculation\n"); scanf("%1f%c%1f",&number_f,&operation,&number_s); switch(operation) { case '+': printf("You answer is %1f\n",number_f+number_s); break; case '-': printf("You answer is %1f\n ",number_f-number_s); break; case '/': if(number_s==0) printf("the number s you put in is wrong\n"); else printf("You answer is %1f \n",number_f/number_s); break; case '*': printf("You answer is %1f\n" ,number_f*number_s); break; } printf("DO you want anymore\n"); printf("Please Edit Y/N\n"); scanf(" %c",&choice_1); } while(toupper(choice_1)=='Y'); return 0; }
红金龙-时光 2013-05-15
  • 打赏
  • 举报
回复
红金龙-时光 2013-05-15
  • 打赏
  • 举报
回复
#include<ctype.h>
int main(void)
{

float number_f;
float number_s;
char operation;
char choice_1;
do
{
printf("Please Edit the chaculation\n");
scanf("%f%c%f",&number_f,&operation,&number_s);/////////////////////////
switch(operation)
{
case '+':
printf("You answer is %1f\n",number_f+number_s);
break;
case '-':
printf("You answer is %1f\n ",number_f-number_s);
break;
case '/':
if(number_s==0)
printf("the number s you put in is wrong\n");
else
printf("You answer is %1f \n",number_f/number_s);
break;
case '*':
printf("You answer is %1f\n" ,number_f*number_s);
break;
}
printf("DO you want  anymore\n");
printf("Please Edit Y/N\n");
//fflush(stdin);///////////////////////////////////////
scanf(" %c",&choice_1);

}
while(choice_1 == 'y' || choice_1 == 'Y');///////////////////////

return 0;
}
非常感谢各位大大 好像可以了
u010423430 2013-05-15
  • 打赏
  • 举报
回复
scanf("%c",&choice_1); 什么意思?
AnYidan 2013-05-15
  • 打赏
  • 举报
回复
scanf("%c",&choice_1); scanf(" %c",&choice_1);
AnYidan 2013-05-15
  • 打赏
  • 举报
回复
scanf(" %c",&choice_1);
u010423430 2013-05-15
  • 打赏
  • 举报
回复
为什么要继续使用getchar ? 我改为getchar 确实可以输入y/n了 但是 while循环判定 仍然不好使 是什么原因 感谢各位
huster_yjt 2013-05-15
  • 打赏
  • 举报
回复
#include<stdio.h> #include<ctype.h> int main(void) { float number_f; float number_s; char operation; char choice_1; do { printf("Please Edit the chaculation\n"); scanf("%f%c%f",&number_f,&operation,&number_s);///////////////////////// switch(operation) { case '+': printf("You answer is %1f\n",number_f+number_s); break; case '-': printf("You answer is %1f\n ",number_f-number_s); break; case '/': if(number_s==0) printf("the number s you put in is wrong\n"); else printf("You answer is %1f \n",number_f/number_s); break; case '*': printf("You answer is %1f\n" ,number_f*number_s); break; } printf("DO you want anymore\n"); printf("Please Edit Y/N\n"); fflush(stdin);/////////////////////////////////////// scanf("%c",&choice_1); } while(choice_1 == 'y' || choice_1 == 'Y');/////////////////////// return 0; } 加斜线的是改的
iBelie 2013-05-15
  • 打赏
  • 举报
回复
#include<stdio.h> #include<ctype.h> int main(void) { float number_f; float number_s; char operation; char choice_1; do { printf("Please Edit the chaculation\n"); scanf("%1f%c%1f",&number_f,&operation,&number_s); switch(operation) { case '+': printf("You answer is %1f\n",number_f+number_s); break; case '-': printf("You answer is %1f\n ",number_f-number_s); break; case '/': if(number_s==0) printf("the number s you put in is wrong\n"); else printf("You answer is %1f \n",number_f/number_s); break; case '*': printf("You answer is %1f\n" ,number_f*number_s); break; } printf("DO you want anymore\n"); printf("Please Edit Y/N\n"); getchar();//用来接收你最后输入的回车字符 scanf("%c",&choice_1); }while(toupper(choice_1)=='Y');//字符与上面的(Y/N)保持一致 return 0; }

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧