新手请教scanf()自动跳过的问题

K-2SO 2012-09-29 06:25:04
先上代码

#include "stdio.h"
#include "stdlib.h"
#include "math.h"

double teacher_total(double _amount)
{
double total;
printf("Total purchases $%6.2lf\n", _amount);
if(_amount >= 100)
{
total = _amount * (1 - 0.12);
printf("Teacher's discount (12%%) %6.2lf\n", _amount * 0.12);
printf("Discounted total $%6.2lf\n", total);
}
else{
total = _amount * (1 - 0.10);
printf("Teacher's discount (10%%) %6.2lf\n", _amount * 0.10);
printf("Discounted total $%6.2lf\n", total);
}
printf("Sales tax (5%%) %6.2lf\n", total * 0.05);
printf("Total $%6.2lf\n", total * 1.05);
}
double non_teacher(double _amount)
{
printf("Total purchases $%6.2lf\n", _amount);
printf("Sales tax (5%%) %6.2lf\n", _amount * 0.05);
printf("Total $%6.2lf\n", _amount * 1.05);
}
int main()
{
double amount;
char is_teacher;
printf("KEITH'S SHEET MUSIC CALCULATOR\n");
printf("Please enter the total amount for purchase ($):\n");
scanf("%lf", &amount);
printf("Are you a music teacher? ");
scanf("%c", &is_teacher);
printf("\n");
printf("RECEIPT\n");
if(is_teacher == 'Y')
{
teacher_total(amount);
}
else{
non_teacher(amount);
}
system("PAUSE");
return 0;

}

扫描is_teacher的那个scanf每次都跳过然后就运行else里面的non_teacher,不知道怎么回事。 把is_teacher换成int 'Y'变成1就没有问题。是我scanf里面的书写有问题吗?
...全文
275 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
AndyZhang 2012-09-30
  • 打赏
  • 举报
回复
scanf独到空格会结束读取。
如果想读取一行,请用gets
AnYidan 2012-09-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
引用 6 楼 的回复:

因为 %s 不读取空白字符 包括 制表符空格符回车符等等
然后换成%s是不可取的 %s会多写入一个字符'\0' 但是你只提供了一个char
错误类似数组越界

"%c" 换成 " %c"注意有一个空格 就行了

在扫描%d %f这样的数字之后再扫描%c都会出现这种问题吗,为什么连着扫描几个%d或%f就不会这样呢?
[/Quote]

google "white space"
K-2SO 2012-09-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

因为 %s 不读取空白字符 包括 制表符空格符回车符等等
然后换成%s是不可取的 %s会多写入一个字符'\0' 但是你只提供了一个char
错误类似数组越界

"%c" 换成 " %c"注意有一个空格 就行了
[/Quote]
在扫描%d %f这样的数字之后再扫描%c都会出现这种问题吗,为什么连着扫描几个%d或%f就不会这样呢?
lin51616780 2012-09-29
  • 打赏
  • 举报
回复
因为 %s 不读取空白字符 包括 制表符空格符回车符等等
然后换成%s是不可取的 %s会多写入一个字符'\0' 但是你只提供了一个char
错误类似数组越界

"%c" 换成 " %c"注意有一个空格 就行了
冷月清晖 2012-09-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

C/C++ code

#include "stdio.h"
#include "stdlib.h"
#include "math.h"

double teacher_total(double _amount)
{
double total;
printf("Total purchases $%6.2lf\n"……
[/Quote]

或者用 fflush(stdin);清空下缓存。
K-2SO 2012-09-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

C/C++ code

#include "stdio.h"
#include "stdlib.h"
#include "math.h"

double teacher_total(double _amount)
{
double total;
printf("Total purchases $%6.2lf\n"……
[/Quote]

懂起~谢谢!那为什么改成%s也可以了呢
左眼看到鬼 2012-09-29
  • 打赏
  • 举报
回复

#include "stdio.h"
#include "stdlib.h"
#include "math.h"

double teacher_total(double _amount)
{
double total;
printf("Total purchases $%6.2lf\n", _amount);
if(_amount >= 100)
{
total = _amount * (1 - 0.12);
printf("Teacher's discount (12%%) %6.2lf\n", _amount * 0.12);
printf("Discounted total $%6.2lf\n", total);
}
else{
total = _amount * (1 - 0.10);
printf("Teacher's discount (10%%) %6.2lf\n", _amount * 0.10);
printf("Discounted total $%6.2lf\n", total);
}
printf("Sales tax (5%%) %6.2lf\n", total * 0.05);
printf("Total $%6.2lf\n", total * 1.05);
}
double non_teacher(double _amount)
{
printf("Total purchases $%6.2lf\n", _amount);
printf("Sales tax (5%%) %6.2lf\n", _amount * 0.05);
printf("Total $%6.2lf\n", _amount * 1.05);
}
int main()
{
double amount;
char is_teacher;
printf("KEITH'S SHEET MUSIC CALCULATOR\n");
printf("Please enter the total amount for purchase ($):\n");
scanf("%lf", &amount);
printf("Are you a music teacher? ");

getchar(); //这里需要把回车换行符从缓存中拿走,你的scanf直接跳出不执行就是因为缓存中还有数据你第一个scanf时留下的'\n',第二个scanf直接就拿'\n'作为它需要的数据了,然后is_teacher == '\n',然后就直接执行else里面的 non_teacher(amount);所以你需要把第一次scanf后的缓存中的'\n'拿走。可以用getchar();

scanf("%c", &is_teacher);
printf("\n");
printf("RECEIPT\n");
if(is_teacher == 'Y')
{
teacher_total(amount);
}
else{
non_teacher(amount);
}
system("PAUSE");
return 0;

}
左眼看到鬼 2012-09-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

好吧我自己解决了把%c换成%s就好了o.0
[/Quote]
楼主起来的好早啊,好好学习,天天向上
K-2SO 2012-09-29
  • 打赏
  • 举报
回复
好吧我自己解决了把%c换成%s就好了o.0

69,371

社区成员

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

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