C程序的奇怪行为

时间:2012-05-10 14:04:45

标签: c

以下是我的C代码。

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

int main()
{
    int ch;

    do
    {
        printf("\n1.create\n2.display\n3.exit\n\t");
        printf("Enter your choice:: ");
        scanf("%d",&ch);
        printf("choice = %d\n\n", ch);
        if(ch==32767)
            return 0;
        switch(ch)
        {
            case 1:
                printf("\n Case 1 executed\n");
                break;
            case 2:
                printf("\nCase 2 executed\n");
                break;
            case 3:
                printf("\nExit\n");
                exit(0);
                break;
            default:
                printf("Wrong choice!!!");
                break;

        }
    }while(ch!=3);
    return 0;
}

问题是,当我为ch输入整数值时,它工作正常。 但是当我输入任何字符时,它会以无限循环运行。

任何人都可以解决它。

2 个答案:

答案 0 :(得分:2)

如果您希望能够处理字符,则应该使用char值而不是int

在这种情况下,您还必须修改switch-case语句,因为'1' - 一个字符与1不同 - 一个整数。更正后的代码应该是:

#include <limits.h>

int main()
{
    char ch;

    do
    {
        printf("\n1.create\n2.display\n3.exit\n\t");
        printf("Enter your choice:: ");
        scanf("%c",&ch);
        printf("choice = %c\n\n", ch);
        switch(ch)
        {
            case '1':
                printf("\n Case 1 executed\n");
                break;
            case '2':
                printf("\nCase 2 executed\n");
                break;
            // case 32767: - can not be stored in a char variable
            case 127:
            case CHAR_MAX: // these two are almost equivalent, but the
                           // second one is better because it relies on
                           // library defined constant from limits.h
            case '3':
                printf("\nExit\n");
                exit(0);
                break;
            case 'a':
                printf("\nA character case accepted!\n");
                break;
            default:
                printf("Wrong choice!!!");
                break;

        }
    }while();
    return 0;
}

请注意,我还将中断条件排除在while()参数之外,因为它是多余的 - 它已经在switch语句中进行了检查。

我还添加了一个解析字符的非错误情况,以便您可以看到一个例子。

另一个注意事项:您的旧代码应接受011作为有效选择,而新代码将01解析为两个不同的选择(1个选项== 1字符):

  • 0将被解析为错误选择
  • 1将被解析为案例1的正确选择

我还在更正后的代码段中的代码注释中对您的代码中的其他一些内容进行了评论。如果(正如Jerry Coffin在评论中指出的那样),我拿出了“不可能”,并把它放在一个更合适的地方,将常数替换为有意义的东西。

答案 1 :(得分:-2)

使用scanf ("%*[^\n]\n");

这里你使用了 scanf(“%d”,&amp; ch); ,它会正确读取整数值,但只要你给出除整数之外的任何值,它将终止或显示不同的值输出。 Why this is happening ? @它很高兴,因为每次你输入一些有效或无效的值,首先它放在输入缓冲读取器中,然后从那里读取该值。

有关scanf的更多信息:http://en.wikipedia.org/wiki/Scanf_format_string

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

int main()
{
    int ch;

    do
    {
        printf("\n1.create\n2.display\n3.exit\n\t");
        printf("Enter your choice:: ");

        scanf ("%*[^\n]\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
                printf("\n Case 1 executed\n");
                break;
            case 2:
                printf("\nCase 2 executed\n");
                break;
            case 3:
                printf("\nExit\n");
                exit(0);
                break;
            default:
                printf("Wrong choice!!!");
                break;

        }
    }while(ch!=3);
    return 0;
}