C 程序对输入值进行向下舍入

标签 c

我需要帮助

我有这个代码来验证输入:

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

int checkInput1(void);

int main(){

    float a = checkInput1();

    printf("The value of a is:%f\n" , a);
} 

  int checkInput1(void){
    float option1,check1;
    char c;

    do{
        printf("Enter the first side of the triangle");

        if(scanf("%f%c",&option1,&c) == 0 || c != '\n'){
            while((check1 = getchar()) != 0 && check1 != '\n' && check1 != EOF);
            printf("\t[ERR] Invalid number for the triplet.\n");
        }else{
            break;
        }
    }while(1);
    printf("returning the value of option1, which is %f\n", option1);
    return option1;



}

我的问题是,如果我输入例如 78.8

输出将是:

Enter the first side of the triangle78.8                                                                 
returning the value of option1, which is 78.800003                                                       
The value of a is:78.000000

我想说的是:

Enter the first side of the triangle78.8                                                                 
returning the value of option1, which is 78.800003                                                       
The value of a is:78.800000

我做错了什么?

提前致谢

最佳答案

您将从函数返回 int,这是不包含小数类型的整数类型。查看您的声明:int checkInput1(void)。函数名称 (checkInput1) 之前的 int 表示返回的类型。考虑在那里使用其他类型。

关于C 程序对输入值进行向下舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46977982/

相关文章:

c - 了解 C 中的函数原型(prototype)

c - 计算矩阵乘法时出现段错误(核心转储)错误

c - C中读取空字符串的方法

c - C 变量中需要的垃圾值

c - C中定义的数组在哪里,当我将它作为函数参数传递时,为什么不必重新定义它?

c - 如何在 C 中编辑 char* 字符串的值?

c - 一个只调用另一个函数的函数会减慢速度吗?

当运行时计算结果为 NaN 或 inf 时,我可以让 gcc 告诉我吗?

c - 如何向其他线程发送包含更多信息的信号?

c++ - 如何使用顶点法线为 GL_POINTS 启用两侧照明