本文介绍了While循环跳过它的病情scanf函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么While循环不仅仅是速度,距离跳过scanf函数的字符?
它甚至不问我的投入,只是环像没有明天。

I can't see why does the While loop just speed away and skipping the scanf for the char?It would not even ask for my input and just loop like there's no tomorrow.

#include <stdio.h>


int main()
{
    int number;
    int multiply, ans;
    char choice;

    printf("-------------------------------------");
    printf("\n      MULTIPLICATION TABLE           ");
    printf("\n-------------------------------------");


    do
    {

         printf("\nEnter an integer number:");
         scanf("%d", &number);


        printf("\nMultiplication of %d is :-\n", number);
        printf("\n");

        for(multiply=1; multiply<11; multiply++){
            ans = number * multiply;
            printf(" %d", ans);
        }

        printf("\n");
        printf("\nWould you like to continue? [Y] for Yes,[N] for no : ");
        scanf("%c", &choice);
        printf("\n");

    }
    while(choice='Y');

    printf("Thank You");
    return 0;

}

推荐答案

scanf()的不会做你认为它(换行字符,缓冲等)。这是pferred使用$ P $ 龟etc()

scanf() doesn't do what you think it does (newline characters, buffering, etc.). It's preferred to use fgetc():

choice = fgetc(stdin);

出于同样的原因,你需要摆脱尾随换行符的

For the same reason, you need to get rid of the trailing newline that

scanf("%d", &number");

叶在标准输入缓冲区。为了解决这个问题,插入

leaves in the standard input buffer. To fix this, insert

fgetc(stdin);

scanf函数是特定的呼叫后()

另外,C是不帕斯卡。平等比较运营商 - 和条件 - 你要找的是

Also, C is not Pascal. The equality comparison operator - and the condition - you're looking for is

while (choice == 'Y')

单方程标记表示赋值。

the single equation mark denotes an assignment.

这篇关于While循环跳过它的病情scanf函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 16:22
查看更多