开关的情况下不是在C正常工作 [英] switch case not working properly in C

查看:104
本文介绍了开关的情况下不是在C正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序有一个小问题。

当我preSS 2或3或4它将显示正常,但之后,当我
preSS A或B或C等,它会显示previous一个结果,而不是打印的选项无效。

我该如何解决这个问题?

 的#include<&stdio.h中GT;
    #包括LT&;&string.h中GT;    typedef结构车辆
    {
    字符名称[100];
    烧焦lice_no [25];
    INT汽车类型;
    炭cmpny_name [100];
    INT menu_year;
     }记录;    INT主要(无效)
    {
    INT I,选择;
    FILE * FP1,FP2 *;
    烧焦ONAME [100];
    记录DET,DET1;
    INT recsize来;
    焦炭℃;    FP1 = FOPEN(record.dat,R +);
    如果(FP1 == NULL)
    {
        FP1 = FOPEN(record.dat,W +);
        如果(FP1 == NULL)
        {
            的printf(错误打开文件:\\ n);
            返回-1;
        }
    }
    recsize来的sizeof =(DET);    做
    {
        的printf(\\ t \\输入选择\\\\ n);        的printf(1:添加记录\\​​ n);
        的printf(2:删除记录\\ n);
        的printf(3:编辑记录\\ n);
        的printf(4:显示记录\\ n);
        的printf(5:退出程序\\ n);
        fflush(标准输入);
        scanf函数(%d个,&安培;选择);
        scanf函数(%C,和C);        开关(选择)
        {
            情况1 :
            {
                    的printf(在这个添加逻辑\\ n)
                打破;
            }
            案例2:
            {
                的printf(在这种情况下删除逻辑\\ n);
                打破;
            }
            案例3:
            {
                的printf(在这种情况下编辑逻辑\\ n);
                                打破;
            }
            情况4:
            {
                的printf(显示逻辑\\ n);
                打破;
            }
            情况5:
            {
                的printf(退出逻辑\\ n);
                打破;
            }
            默认情况下:
            {
                的printf(\\无效选项\\\\ n);
                打破;
            }
        }
    }
    而(1);
    返回0;
}


解决方案

它看起来像你所得到的数字为选择,而字符为C。
但您只使用交换机的选择VAR,你永远不会检查的C变种。

所以基本上,如果你打一个字母是存放在C变种,然后再次使用选择旧值。

In my program there is a minor problem.

When I press 2 or 3 or 4 it will display the properly but after that when I press the a or b or c etc., it will display previous one result instead of print the Invalid option.

How can I fix this?

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

    typedef struct vehicle
    {
    char name[100];
    char lice_no[25];
    int vehicle_type;
    char cmpny_name[100];
    int menu_year;
     }record;

    int main(void)
    {
    int i,choice;
    FILE *fp1,*fp2;
    char oname[100];
    record det,det1;
    int recsize;
    char c;

    fp1 = fopen("record.dat" , "r+");
    if(fp1 == NULL)
    {
        fp1 = fopen("record.dat" , "w+");
        if(fp1 == NULL)
        {
            printf("error in opening file : \n");
            return -1;
        }
    }
    recsize = sizeof(det);

    do
    {
        printf("\t\"enter the choice\"\n");

        printf("1 : adding the record\n");
        printf("2 : delete the record\n");
        printf("3 : editing the record\n");
        printf("4 : display the record\n");
        printf("5 : exit the program\n");


        fflush(stdin);
        scanf("%d" , &choice);
        scanf("%c" , &c);

        switch(choice)
        {
            case 1 :
            {
                    printf("In this add logic\n")
                break;
            }
            case 2 :
            {
                printf("In this case delete logic\n");
                break;
            }
            case 3 :
            {
                printf("In this case edit logic\n");
                                break;
            }
            case 4 :
            {
                printf("display logic\n");
                break;
            }
            case 5 :
            {
                printf("exit logic\n");
                break;
            }
            default :
            {
                printf("\"Invalid option\"\n");
                break;
            }
        }
    }
    while(1);
    return 0;
}

解决方案

It looks like you are getting the numbers into Choice, and the chars into c. but you are only using the Choice var in the switch, you are never checking the C var.

So essentially, if you hit a letter it is storing it in the C var, and then using the old value in Choice again.

这篇关于开关的情况下不是在C正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆