C程序将阶乘值返回到33个数字,并且在将字符作为输入后输出将进入无限循环 [英] C program is returning factorial value upto 33 number and output is going in infinite loop after giving character as an input

查看:83
本文介绍了C程序将阶乘值返回到33个数字,并且在将字符作为输入后输出将进入无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个因子C程序,其中不允许用户输入负数和字母字符,但允许从0开始的数字。程序将询问输入,直到按下回车键。



当我输入任何数字或按回车键时程序正常工作但是当我输入任何字符时它会进入无限循环给出输出为阶乘1为1 。为什么会发生这种情况?



当我输入字母字符的ASCII值(例如109)时,我输出为不允许使用字母但期望值是该数字的因子值。我知道这是因为我使用相同的变量来检查字母和数字。那我该如何分别检查字母和数字?因为输入值存储在一个变量中。



在输出中我得到的因子最多只有33个数。如果我输入34或任何大于34的数字,那么我得到的输出为[输入数字]的阶乘为0。为什么对于大于33的数字,因子值为0?



请指导我在哪里犯错误。



我尝试了什么:



I am writing one factorial C program in which user is not allowed to enter negative and alphabet character but number from 0 is allowed. The program will ask for input until enter key is pressed.

The program works fine when I enter any number or press enter key but when I enter any character it goes into infinite loop giving output as "factorial of 1 is 1". Why is this happening?

Also when I enter ASCII value of alphabet character(e.g. 109) I am getting output as "alphabet is not allowed" but the expectation is factorial value of that number. I know this is because I am using same variable to check for alphabet and number. Then how do I check for alphabet and number separately? as input value is stored in one variable.

In output I am getting factorial upto 33 number only. If I enter 34 or any number greater than 34 then I am getting output as "factorial of [entered number] is 0". Why the factorial value is 0 for number greater than 33?

Kindly guide me where I am making mistake.

What I have tried:

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

int display();
void fact_fun(int num_fact);

int main()
{
    int num = 0;
    char next;

    next=display();

    while( next != '\n')
    {
        num = next;
        fact_fun(num);
        next=display();
    }

    return 0;
}

int display()
{
    int input;
    printf("\nEnter number to find factorial or press ENTER to exit :");
    scanf("%d",&input);

    return input;
}

void fact_fun(int num_fact)
{
    int fact=1;
    if((num_fact>='a' && num_fact<='z') || (num_fact>='A' && num_fact<='Z'))
    {
        printf("Alphabet is not allowed");
    }
    else if(num_fact < 0)
    {
        printf("Negative number is not allowed");
    }
    else if(num_fact == 0)
    {
        printf("Factorial of %d is 1",num_fact);
    }
    else
    {
        for(int i = 1; i<= num_fact; i++)
        {
            fact = fact*i;
        }
        printf("Factorial of %d is %d",num_fact,fact);
    }
}

推荐答案

问题是你没有做任何错误检查: C库函数 - scanf() [ ^ ]如果输入错误值并且您没有检查此值,则返回一个负数,您只需假设<$ c中的值$ c>输入始终正确且有效。



修改显示功能检查响应,并重复直到用户输入正确的内容。并改名称!它不会显示它提示输入并从用户获取值。



但这不是你唯一的问题:因为你使用 scanf 带有数字代码 - %d - 它永远不会返回'\ n',所以赢了; t也工作得很好。



我建议你使用 scanf 从用户那里读取一行字符,然后检查是否为空 - 如果它是空的,然后用户按下ENTER而没有数字,你就完成了。如果不是,则使用 C库函数 - atoi() [ ^ ]尝试将其转换为整数。然后你可以抛弃你的AZ检查并继续使用阶乘。



请注意,阶乘变得非常大,非常快:它们将超过32位n到达33之前的整数!
The problem is that you don't do any error checking: C library function - scanf()[^] returns a negative number if a "bad value" is entered and you do not check for this, you just assume that the value in input is always correct and valid.

Modify your display function to check the response, and repeat until the user types something right. And change its name! It doesn't "display" it prompts for input and gets a value from the user.

But that's not your only problem: because you use scanf with a numeric code - "%d" - it will never return '\n', so that won;t work too well either.

What I'd suggest is that you use scanf to read a line of characters from the user, and then check that for "empty" - if it's empty, then the user pressed ENTER without a number and you are done. If it isn't, then use C library function - atoi()[^] to try and convert it to an integer. You can then throw out your A-Z checking and just get on with the factorial.

Do be aware that factorials get very large, very quickly: they will exceed an 32 bit integer long before n reaches 33!


您需要检查 scanf 调用的结果,以确保用户输入有效数。请参阅 scanf,_scanf_l,wscanf,_wscanf_l [ ^ ]来自 scanf 打电话。另请注意, display 函数将永远不会返回'\ n'字符,因为它只接受整数输入。您需要使用特殊值来表示结束。



此代码是多余的:

You need to check the result of your scanf call to make sure that the user typed a valid number. See scanf, _scanf_l, wscanf, _wscanf_l[^] for the return value from a scanf call. Note also that your display function will never return the '\n' character, as it only accepts integer input. You need to use a special value to indicate the end.

This code is redundant:
if((num_fact>='a' && num_fact<='z') || (num_fact>='A' && num_fact<='Z'))
{
    printf("Alphabet is not allowed");
}



您已经有一个整数值(在 display 函数中检查) ,所以将它与任意数字范围进行比较并不意味着你的想法。


You already have an integer value (checked in the display function), so comparing it to an arbitrary range of numbers does not mean what you think.


这篇关于C程序将阶乘值返回到33个数字,并且在将字符作为输入后输出将进入无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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