酒话醉人丶 2016-10-18 10:28 采纳率: 0%
浏览 2317

新人求教c语言问题,出现switch函数没有起作用,直接跳到下一步

部分代码如下,在Choose函数中:想使用一个循环,结果在第一个那个部分进行选择时出现错误,
选择Choicepass()函数后,Choicepass()函数仅仅执行到"scanf("%c",&M);",
再直接进入Choose函数中的" printf("是否重新进行?\nc->继续\ne->退出:");"这条语句。
想要知道为什么,求大神为新手求解,万分感谢!
结果如图:
图片说明
int Choicepass()

{
int n,T,L,S1,S2,S3;
char M;
printf("Please choose the model:\na->easy\nb->normal\nc->difficulty\n:");
scanf("%c",&M);

switch(M)                                                        //选择难度所用函数
{
case 'a':
    {
        srand(time(0));
        n = rand()%10+1;
        T = 3;
        S1 = Guess1(n,T);
    }break;
case 'b':
    {
        srand(time(0));
        n = rand()%50+1;
        printf("You should get the answer from 1~50.\nPlease choose the TimesLimit(5~10):");
        scanf("%d",&T);
        S2 = Guess2(n,T);
    }break;
case 'c':
    {
        srand(time(0));
        n = rand()%100+1;
        printf("You should get the answer from 1~100.\nPlease choose the TimesLimit(5~15):");
        scanf("%d",&T);
        S3 = Guess3(n,T);
    }break;
}
L = 1;
return L ;

}

    /*选择关卡模式或者选择难度模式*/

int Choose()
{
int L,T;
char k,h;

printf("选择关卡模式或者自由选择难度模式:\ng->关卡\nz->自由:");
scanf("%c",&h);

if(h == 'c')

    L = Customspass();

else if(h == 'z')

    L = Choicepass();

else printf("ERROR!\n");

printf("是否重新进行?\nc->继续\ne->退出:");
scanf("%c",&k);

if(k == 'c')     

    T = Choose();

else     
    printf("游戏结束!\n");

return T;

}

  • 写回答

1条回答 默认 最新

  • hander_left 2016-10-18 11:26
    关注

    没太看懂你的问题。如果我没有理解错的话,应该是缓冲区问题。
    在choose()函数中的scanf(“%c”,&h)后面加一句getchar()试试

    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容