读取用户命令继续不起作用 [英] reading user command to continue does not work

查看:116
本文介绍了读取用户命令继续不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了计费系统的程序。我用我的程序do-whil​​e循环。和程序,根据用户的输入执行。如果用户要继续执行,程序将继续进行。但我得到了执行中的概率。我试图用简单的do-whil​​e循环我的逻辑。同样的问题也出现了简单的do-whil​​e循环也。

I'm Writing a program for Billing System. I'm using do-while loop in my program. And the program is executed according to user input. If the user want to continue the execution, the program will be continue. But I Got a prob in Execution. I was trying my logic in simple do-while loop. The same Problem arises for simple do-while loop also.

Problem is: If the input is yes, the program does not get the further input from user.

这是简单的do-whil​​e循环是:

That simple do-while loop is:

#include <stdio.h>

main()
{
    int c;
    char ch;
    do
    {
            printf("enter the no less then 4:");
            scanf("%d",&c); 
        switch(c)
        {
            case 1:
                printf("In 1\n");
                break;
            case 2:
                printf("In 2\n");
                break;
            case 3:
                printf("In 3\n");
                break;
        }
        printf("do u want to continue?:");
        ch=getchar();
    }while(ch=='y');
}

如果我把而(CH!='N')而不是,而(CH =='Y')程序工作正常。我无法理解这背后的问题。请帮我纠正。并解释这个problem.Thankü提前。

If i put while(ch != 'n') instead of while(ch=='y') the program working fine. I couldn't understand the problem behind this. Please Help me to rectify this. And Explain about this problem.Thank u in advance.

推荐答案

第一次运行,3打印,用户键入Y和presses返回

first run, 3 is printed, user types "y" and presses return

的getchar()读取'Y'和程序循环

getchar() reads 'y' and program loops

第二次的getchar()从$ P $读取换行符pvious键preSS

second time, getchar() reads newline character from the previous key press

新行不是'Y'这样的程序不循环

newline is not 'y' so program does not loop

这篇关于读取用户命令继续不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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