【问题标题】:using switch case, error on the default part使用开关盒,默认部分出错
【发布时间】:2011-11-07 15:47:57
【问题描述】:

虽然默认部分不能运行, 它运行所有案例,我现在感谢所有帮助,我该怎么办?此代码全部运行我在屏幕默认情况下编写。

do
{
scanf("%c",&choice);

switch(choice)
{
    case 'T':
        printf("Enter edges of Triangle1\n");
        scanf("%d%d",&edge1,&edge2);
        Triangle1(edge1,edge2);
        break;
    case 't':
        printf("Enter edges of Triangle1\n");
        scanf("%d%d",&edge1,&edge2);
        Triangle2(edge1,edge2);
        break;
    case 'R':
        printf("Enter edges of square\n");
        scanf("%d%d",&edge1,&edge2);
        Rectangle(edge1,edge2);
        break;
    case 'S':
        printf("Enter one edge of square\n");
        scanf("%d",&edge);
        Square(edge);
        break;
    case 'C':
        printf("Enter radius of circile\n");
        scanf("%d",&radius);
        Circle(radius);
        break;
    default:
        printf("Wrong input\n");
        break;
}//end of switch
printf("\n");
}while(choice!='e');

【问题讨论】:

  • 这可能是%d 留在缓冲区中的\n
  • 预期输入可能是“T,t,S,R,C”
  • 他们的意思是问,你输入的是什么?你也可以printf 使用scanf 读取的值很可能是\n
  • 我要求用户输入任何内容,程序运行默认行。当用户输入错误输入时,此时默认行应该运行。我要这个。我该怎么办?

标签: c case switch-statement


【解决方案1】:

换行符和输入一样被传递。尝试在第一个 scanf() 之后调用“getc(stdin)”,它应该可以工作:)

查看例如:http://home.datacomm.ch/t_wolf/tw/c/getting_input.html#newline

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2013-01-01
    • 2021-03-30
    • 2011-05-15
    • 2023-03-14
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多