【问题标题】:Something wrong with character input and switch case in C?C中的字符输入和切换大小写有问题吗?
【发布时间】:2016-01-20 16:11:08
【问题描述】:

有一个问题,它取 no: 的情况,在每种情况下都取 no: 的对象,每个对象要么是点,要么是圆或线段。我已经编写了完整的代码,但开关盒不起作用。任何建议表示赞赏。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
    int test,nob,ans[4],px,py,cx,cy,cr,lx1,ly1,lx2,ly2,flag,i,j;
    char type;

    scanf("%d",&test); //no:of test cases

    while(test) {

        flag=0;
        scanf("%d",&nob); //no:of objects
        while(nob)
        {
            scanf("%c\\n",&type); //what type of object
            switch(type)
            {
            case 'p':
                {
                    scanf("%d%d",&px,&py);
                    if((px<=ans[0] && py<=ans[1] && px>=ans[2] && py>=ans[3])|| flag==0) {
                        ans[0]=px;ans[1]=py;ans[2]=px;ans[3]=py;
                    }
                    break;
                }
            case 'c':
                {
                    scanf("%d%d%d",&cx,&cy,&cr);
                    if((cx-cr<=ans[0] && cy-cr<=ans[1] && cx+cr>=ans[2] && cy+cr>=ans[3])|| flag==0) {
                        ans[0]=cx-cr;ans[1]=cy-cr;ans[2]=cx+cr;ans[3]=cy+cr;
                    }
                    break;
                }
            case 'l':
                {
                    scanf("%d%d%d%d",&lx1,&ly1,&lx2,&ly2);
                    if((lx1<=ans[0] && ly1<=ans[1] && lx2>=ans[2] && ly2>=ans[3])|| flag==0) {
                        ans[0]=lx1;ans[1]=ly1;ans[2]=lx2;ans[3]=ly2;
                    }
                    break;
                }
            default:
                printf("you entered wrong letter.");
            }
            flag=1;
            nob--;
        }
        for(i=0;i<4;i++)
        {
            printf("%d ",ans[i]);
        }
        printf("\n");
        test--;
    }
    return 0;
}

当您运行此程序时,switch case 不起作用。我认为输入类型字符有问题。请帮助我。我已经尝试解决这个错误 3 天了。

【问题讨论】:

  • 答案解释了故障,但当错误条目导致default 情况时,是否也应该不设置flagnob
  • @Weather vane,对不起..我不明白你问了什么。
  • 我是说,在switch 语句之后,您修改了flagnob。但是如果有一个无效的输入,导致default: 的情况,你还想要这两个变量更新吗?如果没有,请将它们移至其他每个人(成功)case 语句。查看答案的更新。
  • 是的,我不认识它。答案的更新是正确的。我会改变它。谢谢。

标签: c switch-statement character scanf


【解决方案1】:

%c 之前必须有一个空格 - 这里也不需要 \\n

scanf(" %c", &type); //what type of object

否则,前一个scanf() 中的newline (\n) 将被读入为type%d 不需要)。

你还需要定义ans:

int ans[4] = {0};

并且j 没有被使用——可以移除:

'j' : unreferenced local variable

此外,正如Weather Vane 所述,如果typeflagnob 的输入错误,则不应更新。您可以按如下方式解决此问题:

default :
    printf("you entered wrong letter.");
    continue;
}
flag=1;
nob--;

【讨论】:

  • 这对我的代码有很大帮助并完全纠正了我的代码。你能解释一下为什么我们应该在 %c 之前保留空间。
  • %c 之前的空格会占用输入缓冲区中的所有前导空格。在您的情况下,它是 newline%d 条目之后留下的。 %d%s 或其他类型不需要它,它们会自动使用前导空格。我讨厌scanf
  • @harry - 对于之前的 scanf()nob,您必须按 Enter 键才能结束扫描。问题是输入的换行符仍在缓冲区中,下一个扫描单个字符的scanf() 将首先在输入中读取。所以type 的值将是 '\n'。
  • 非常感谢你。它的信息非常丰富和准确。你真的值得称赞,我已经给了它。再见
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-01
  • 2021-05-10
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多