开关盒在C中不能正常工作

时间:2012-05-30 10:36:33

标签: c

在我的计划中存在一个小问题。

当我按下2或3或4时,它会正确显示,但在此之后我会显示 按a或b或c等,它将显示前一个结果,而不是打印无效选项。

我该如何解决这个问题?

    #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;
}

4 个答案:

答案 0 :(得分:2)

看起来你正在将数字输入到Choice中,而将这些数字转换为c。 但是你只在开关中使用Choice var,你永远不会检查C var。

基本上,如果你点击一个字母,它会将它存储在C var中,然后再次使用Choice中的旧值。

答案 1 :(得分:2)

嗯,你的代码中有一个错误的地方是:

scanf("%c" , &c);

因为,scanf函数要求用户在将字符存储到其各自的变量之前按Enter键。

因此,如果编译器读取该行:

scanf("%c" , &c);

它读取您的输入,PLUS,ENTER 因此强制scanf函数将输入,PLUS和ENTER存储到字符变量中。

如果您使用getche()或getch()而不是使用scanf()函数会更好,请不要使用:

scanf("%c" , &c);

因为它会产生错误。


getche()或getch()函数的使用示例:

c=getche(); //waits for a keypress and stores it on a variable
c=getch();  //waits for a keypress and stores it on a variable

两者之间的区别在于getche()显示你的按键,而getch()没有。


注意:别忘了放

#include<conio.h>

添加信息: 如果您仍想继续使用scanf()函数,请确保将您喜欢的变量声明为:

char c[20];

然后你可以使用:

scanf("%s", &c);

但是你的变量最多只能容纳19个字符,就像我们在你的字符数组中声明的那样。

摘要不使用:

scanf("%c", &c);

因为它会影响您的其他scanf()函数。 :)


解决方案(扰流板警告):

#include <stdio.h>
#include <string.h>
#include <conio.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; //removed choice from int
    FILE *fp1,*fp2;
    char oname[100];
    record det,det1;
    char choice; //made the variable choice a character
    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);
    choice = getche(); // or getch()

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

您还可以使用开关来比较字符。只需更改值

即可
case 1:

case '1':

答案 2 :(得分:1)

scanf会返回一个值,您不会检查该值。

%d说明符一起使用时 - 它应解析整数。由于您输入了非整数值 - scanf会返回错误代码,choice不会更改

答案 3 :(得分:0)

这是因为在c中读取整数字符时(scanf(“%d”,&amp; choice);)它取字符的ascii代码,例如a = 97,b = 98 c = 99 d = 100如果你想读取1 b为2等你将不得不添加一些额外的代码告诉程序,如果数字等于abcd的ascii代码或e减去96,那么你得到1,2,3 ..

相关问题