小编ryf*_*rek的帖子

C的strtod()返回错误的值

我目前正在为我的大学的实验室练习制定银行终端计划.

让我摆脱困境的是一个假定用户输入金额的函数,检查它是否符合所有要求,如果是,则返回提供给程序的值.

我们的导师对保护输入的所有方法都很生气,因此我必须在任何不一致的情况下调用错误.价值太高了?错误.负值还是零值?错误.数字比0.01精确?错误.输入中的非数字非点字符?错误.

因此,我的功能肯定过于复杂,但我很好.是什么驱使我走上墙,事实是,两者atof()strtod()函数都以某种错误的方式读取数字.

long double take_amount()
{
    char input[21];
    bool success, correct;
    unsigned long int control1;
    long double amount, control, control2, control3;

    do
    {
        success = true, correct = true;
        printf("\t\tAmount:\t\t");
        success = scanf("%20[^ \t\n]c", input);
       __fpurge(stdin);

        correct = check(input, 'b');

        amount = strtod(input, NULL);

        printf("\n\tGOT %.20Lf\n", amount); ///

        control = amount * 100;
        control1 = (unsigned long int)floor(control);
        control2 = (long double) control1;
        control3 = control2 - control;    

        printf("\n\tGOT2 %.20Lf\n", control3);  ///

        if (control3 …
Run Code Online (Sandbox Code Playgroud)

c string double return-value strtol

2
推荐指数
1
解决办法
1605
查看次数

标签 统计

c ×1

double ×1

return-value ×1

string ×1

strtol ×1