作者marx625001 (Dain)
看板C_and_CPP
标题[问题] switch...case的问题
时间Fri Sep 8 10:23:24 2017
开发平台(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
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
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