C_and_CPP 板


LINE

开发平台(Platform): (Ex: Win10, Linux, ...) Win7 编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出) Dev C++ 额外使用到的函数库(Library Used): (Ex: OpenGL, ...) stdio.h stdlib.h 问题(Question): 为什麽我的switch...case,无法成功载入,会直接跳default:("Error\n); 喂入的资料(Input): 预期的正确结果(Expected Output): printf("Choice your mode: A.Read B.Write C.Randread D.Randwrite E.Randr F.RWmixread=70 \n"); scanf("%c", &mode); 输入A.B.C.D.E等,会printf出来结果 错误结果(Wrong Output): 结果直接跳到Error 程式码(Code):(请善用置底文网页, 记得排版) #include<stdio.h> #include<stdlib.h> int main() { char filename[10]; int iodepth, bs, size, numjobs, runtime; printf("Where is your file? (absult postion)\n"); scanf("%s", filename); printf("Setting your IO module(default=1)\n"); scanf("%d", &iodepth); printf("What is your I/O size(-bs)?\n"); scanf("%d", &bs); printf("How much the test file size\n"); scanf("%d", &size); printf("How many your number of jobs?\n"); scanf("%d", &numjobs); printf("How long do you wany to test?(sec)\n"); scanf("%d", &runtime); char mode; printf("Choice your mode: A.Read B.Write C.Randread D.Randwrite E.Randrw F.RWmixread=70 \n"); scanf("%c", &mode); switch(mode) { case 'A': printf("Fuck u\n"); printf("fio -filename=%s -direct=1 -iodepth %d -thread -rw=read ioengine=libaio -bs=%dm -size=%dG -numjobs=%d -runtime=%d -group_reporting -name=mytest\n", filename, iodepth, bs, size, numjobs, runtime); break; case 'B': printf("fio -filename=%s -direct=1 -iodepth %d -thread -rw=write ioengine=libaio -bs=%dm -size=%dG -numjobs=%d -runtime=%d -group_reporting -name=mytest\n", filename, iodepth, bs, size, numjobs, runtime); break; case 'C': printf("fio -filename=%s -direct=1 -iodepth %d -thread -rw=randread ioengine=libaio -bs=%dm -size=%dG -numjobs=%d -runtime=%d -group_reporting -name=mytest\n", filename, iodepth, bs, size, numjobs, runtime); break; case 'D': printf("fio -filename=%s -direct=1 -iodepth %d -thread -rw=randwrite ioengine=libaio -bs=%dm -size=%dG -numjobs=%d -runtime=%d -group_reporting -name=mytest\n", filename, iodepth, bs, size, numjobs, runtime); break; case 'E': printf("fio -filename=%s -direct=1 -iodepth %d -thread -rw=randrw ioengine=libaio -bs=%dm -size=%dG -numjobs=%d -runtime=%d -group_reporting -name=mytest\n", filename, iodepth, bs, size, numjobs, runtime); break; default: printf("Error\n"); } return 0; } 补充说明(Supplement): --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 60.251.10.52
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1504837410.A.4BB.html
1F:推 Hazukashiine: 你先把 mode 印出来看看啊 09/08 10:32
2F:推 Django: 可能因为code有脏话被和谐了 09/08 10:52
3F:→ marx625001: 呜呜呜 那句话是因为翻烂了一堆C语言的书,还是没结果 09/08 11:02
4F:→ marx625001: 我用printf看了下,mode没答案 09/08 11:04
5F:→ stucode: 大小写? 09/08 11:05
6F:推 stucode: mode没答案? 那八成是吃到换行了 09/08 11:10
7F:推 Django: 应该是runtime的换行被mode吃进去了吧 09/08 11:16
8F:→ marx625001: 吃进去? 那请问要怎麽解决这问题?? 09/08 11:17
9F:→ stucode: 先把换行吃掉再scanf就好 09/08 11:19
10F:→ enonrick: getc(stdin) 09/08 11:20
11F:→ shadow0326: scanf(" %c", &mode); 试试看 09/08 11:20
12F:→ marx625001: s大,我资质余吨,刚入写程式,所以还是有点不太了解 09/08 11:24
13F:→ marx625001: 可以给我关键字之类的,我很愿意自己google学习 09/08 11:24
14F:→ marx625001: enonrick 我还没用过那个指令过,我会上网查的 09/08 11:25
15F:→ marx625001: shadow0326 我刚试过,他还是显示Error 09/08 11:26
16F:→ james732: scanf newline ←用这个google? 09/08 11:28
17F:→ stucode: 一时之间想不到用什麽关键字可以找到相关问题…… 09/08 11:32
18F:→ stucode: 也许可以试试scanf input buffer之类的 09/08 11:32
19F:→ stucode: 简单来说就是上次输入的时候,例如输入123然後[Enter] 09/08 11:33
20F:→ stucode: 输入缓冲区会填入"123\n"。上个scanf只consume掉整数的 09/08 11:34
21F:→ stucode: "123"部分,下一个scanf("%c", ...)读到的就是还留在 09/08 11:36
22F:→ stucode: buffer里面的"\n"。 09/08 11:36
23F:推 moebear: 吃到换行了 scanf前面放一个getchar把它吃掉就好 09/08 11:37
24F:→ stucode: 所以只要想办法把那个"\n"吃掉就好,楼上说的getchar() 09/08 11:39
25F:→ stucode: 就是其中一个方法。 09/08 11:39
26F:推 Django: 或是这样 scanf("%d\n", &runtime); 09/08 11:41
27F:推 shadow0326: https://ideone.com/1c9N0a 为什麽我可以QQ 09/08 11:42
28F:→ Django: 还有是Choose your mode不是Choice... (离题) 09/08 11:42
29F:→ marx625001: 那我想再问一下,到底怎麽看出来的啊?因为我都看书 09/08 11:44
30F:→ marx625001: 书上没解释到这个过 09/08 11:45
31F:→ marx625001: shadow0326 那我再试试看,可能我有地方少打 09/08 11:50
32F:→ moebear: 摁...... IDE可以设中断点观察变数的值 可以发现mode吃到 09/08 11:55
33F:→ moebear: '\n' 之後应该会发现是scanf的问题 去google scanf 换行 09/08 11:56
34F:→ moebear: 就可以找到一些想看的东西 09/08 11:57
35F:→ stucode: 有些入门书会省略细节,没有解释。C Primer Plus中好像 09/08 12:13
36F:→ stucode: 有提到这方面的东西。不过其实主要还是像楼上说的, 09/08 12:14
37F:→ stucode: 用debugger观察然後再google问题。 09/08 12:14
38F:→ marx625001: 感谢各位大大的帮忙,我已经成功可以输出进到switch了 09/08 13:20
39F:→ Lipraxde: scanf前先用fflush(stdin)应该也可以解决这问题 09/08 14:28
40F:→ james732: fflush(stdin)记得不是标准做法 09/08 17:08
41F:→ james732: https://i.imgur.com/A76Phy7.jpg 09/08 17:10
42F:→ Lipraxde: 谢谢J大,受教了 09/08 17:15
43F:→ james732: 有人知道最早做fflush(stdin)的是哪个编译器吗XD 09/08 17:30
44F:→ james732: 其实我觉得所有编译器都做成清除stdin也不错? 09/08 17:30
45F:推 CoNsTaR: 不这样做是因为以前有些硬体的限制没办法 flush stdin 吧 09/08 20:18
46F:→ CoNsTaR: ? 09/08 20:18
47F:推 bben900911: 我自己的习惯上,只用printf大法时 09/08 21:47
48F:→ bben900911: 会在目标变数周围加上[],以防有甚麽肉眼看漏的东西 09/08 21:47
49F:→ bben900911: 被我忽略 09/08 21:47
50F:→ uranusjr: 也有个可能是某些平台的 implementation 就有这个副作用 09/11 13:47
51F:→ uranusjr: 然後被开发人员 exploit 之後就只好一直维护了 09/11 13:47







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP